已知字符串 m_str = “JanFebMarAprMayJunJulAugSepOctNovDec”(十二个月份的英文缩写),根据用户输入的数字查找月份对应的英文缩写。要求:若用户输入数字大于12或小于1时,提示输入错误,请用python代码写程序
时间: 2023-06-08 14:05:18 浏览: 165
好的,我明白了。以下是代码实现:
```
m_str = "JanFebMarAprMayJunJulAugSepOctNovDec"
num = input("请输入一个数字(1-12):")
if num.isdigit():
num = int(num)
if num >= 1 and num <= 12:
month = m_str[(num-1)*3 : num*3]
print("对应的英文缩写为:", month)
else:
print("输入错误,请输入一个数字(1-12)!")
else:
print("输入错误,请输入一个数字(1-12)!")
```
解释一下代码:首先定义了一个字符串变量`m_str`,存储了月份的英文缩写。接着,从用户处获取一个数字`num`。使用`isdigit()`判断输入是否为数字,如果是数字,则将其转换为整型。接下来,判断`num`是否在1到12之间,如果是,则通过字符串的截取来获取对应的月份缩写;否则,输出错误提示信息。如果输入不是数字,则同样输出错误提示信息。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)