用Switch编写,输入年份月份输出天数
时间: 2024-04-29 07:22:53 浏览: 130
```python
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
days = 0
if month in [1, 3, 5, 7, 8, 10, 12]:
days = 31
elif month in [4, 6, 9, 11]:
days = 30
else:
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
days = 29
else:
days = 28
print("{}年{}月的天数为:{}".format(year, month, days))
```
使用Switch语句替代if-elif语句:
```python
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
days = 0
switcher = {
1: 31,
2: 28 if year % 4 != 0 or year % 100 == 0 and year % 400 != 0 else 29,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31
}
days = switcher.get(month, "Invalid month")
print("{}年{}月的天数为:{}".format(year, month, days))
```
阅读全文