请问以下Python编程实现:输入一个1-12的整数,输出对应的月份名称缩写,请考虑数字输入的是否有效。
时间: 2023-05-20 07:03:40 浏览: 177
以下是Python编程实现:
```
month = input("请输入一个1-12的整数:")
if month.isdigit():
month = int(month)
if month >= 1 and month <= 12:
month_names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
print(month_names[month-1])
else:
print("输入的数字不在1-12范围内!")
else:
print("输入的不是整数!")
```
该程序会先判断输入的是否为整数,如果是,则将其转换为整型并判断是否在1-12范围内,如果是,则输出对应的月份名称缩写,否则输出错误信息。如果输入的不是整数,则输出错误信息。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)