2.编写一个名为 check_season 的函数,它接受一个月份参数并返回其对应的季节:秋季、冬季、春季或夏季
时间: 2024-10-16 11:14:27 浏览: 38
你可以创建一个名为 `check_season` 的函数,根据给定的月份来确定季节。这里是一个基于常见的季节划分规则(北半球)的简单实现:
```python
def check_season(month):
seasons = {
9: '秋季',
10: '秋季',
11: '冬季',
12: '冬季',
1: '冬季', # January in the Northern Hemisphere
2: '冬季',
3: '春季', # March is the start of spring in both hemispheres
4: '春季',
5: '春季',
6: '夏季',
7: '夏季',
8: '夏季'
}
return seasons.get(month, "未知") # If month is not in the dictionary, return "未知"
# 测试函数
print(check_season(9)) # 输出:秋季
print(check_season(12)) # 输出:冬季
print(check_season(3)) # 输出:春季
阅读全文