使用列表编写一个程序,输入一个月份,判断该月属于哪个季度
时间: 2024-05-09 21:19:57 浏览: 22
```
# 定义季度列表
quarters = ['春季', '夏季', '秋季', '冬季']
# 获取用户输入的月份
month = int(input('请输入一个月份:'))
# 判断月份所属的季度
if month in [3, 4, 5]:
quarter = quarters[0]
elif month in [6, 7, 8]:
quarter = quarters[1]
elif month in [9, 10, 11]:
quarter = quarters[2]
elif month in [12, 1, 2]:
quarter = quarters[3]
else:
print('输入月份有误,请重新输入!')
quarter = None
# 输出结果
if quarter:
print('{}属于{}'.format(month, quarter))
```
说明:
- 第1行定义了一个季度列表,包含了四个季度的名称。
- 第4行通过`input()`函数获取用户输入的月份,并使用`int()`函数将其转换为整数。
- 第7~16行使用`if-elif-else`语句判断月份所属的季度,并将结果保存在`quarter`变量中。
- 第19~23行根据`quarter`变量是否为空,输出相应的结果。如果输入的月份有误,将不会输出任何结果。
相关问题
使用列表编写一个程序,用户输入一个月份,程序输出该月份对应季度。
好的,这个问题很简单。以下是 Python 代码:
```
month = int(input("请输入月份: "))
if month in [1, 2, 3]:
print("该月份是一季度")
elif month in [4, 5, 6]:
print("该月份是二季度")
elif month in [7, 8, 9]:
print("该月份是三季度")
elif month in [10, 11, 12]:
print("该月份是四季度")
else:
print("无效的月份")
```
希望能对您有所帮助。
使用列表编写一个程序,用户输入一个月份,程序输出该月份对应的季度。
好的,以下是Python代码实现:
```python
month = int(input("请输入一个月份:"))
if month < 1 or month > 12:
print("输入的月份不合法!")
else:
quarters = ["冬季", "春季", "夏季", "秋季"]
quarter = (month - 1) // 3
print(month, "月份属于", quarters[quarter])
```
程序先让用户输入一个月份,然后判断输入的月份是否合法,如果不合法则输出提示信息;如果合法,则根据输入的月份计算出对应的季度,最后输出结果。注意,这里将冬季作为第一个季度,春季作为第二个季度,以此类推。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)