用python循环输入1-12月份,输出对应英文缩写,输入-1时结束循环
时间: 2024-05-22 20:15:32 浏览: 125
month_dict = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'}
while True:
month = int(input('请输入月份(输入-1结束):'))
if month == -1:
break
elif month < 1 or month > 12:
print('输入有误,请重新输入!')
else:
print(month_dict[month])
相关问题
请问以下Python,输入1-12的月份数字,输出对应的月份简称
可以使用以下代码实现:
```
months = {
1: "Jan",
2: "Feb",
3: "Mar",
4: "Apr",
5: "May",
6: "Jun",
7: "Jul",
8: "Aug",
9: "Sep",
10: "Oct",
11: "Nov",
12: "Dec"
}
month_num = int(input("请输入月份数字(1-12):"))
if month_num in months:
print(months[month_num])
else:
print("输入有误,请输入1-12之间的数字")
```
这段代码定义了一个字典,将数字1-12与对应的月份简称进行了映射。然后通过输入一个数字,判断是否在字典中,如果在则输出对应的月份简称,否则输出错误提示。
python输入数字1-12,输出月份简写
在Python中,你可以编写一个简单的函数来将用户输入的1到12之间的数字转换成对应的月份简写。这里是一个示例:
```python
def number_to_month(month_number):
month_dict = {
1: 'Jan', # January
2: 'Feb', # February
3: 'Mar', # March
4: 'Apr', # April
5: 'May', # May
6: 'Jun', # June
7: 'Jul', # July
8: 'Aug', # August
9: 'Sep', # September
10: 'Oct', # October
11: 'Nov', # November
12: 'Dec' # December
}
return month_dict.get(month_number, "Invalid input")
# 获取用户输入并转换
month_input = int(input("请输入1到12的数字代表月份:"))
print(number_to_month(month_input))
阅读全文