从键盘输入某年某月(包括闰年),编程输出该年的该月拥有的天数。 **输入格式要求:"%d,%d" 提示信息:"Input year,month:" "The number of days is %d\n" **输出格式要求:"%d" 程序运行示例1如下: Input year,month:1984,2 The number of days is 29 程序运行示例2如下: Input year,month:2000,2 The number of days is 29 程序运行示例3如下: Input year,month:1985,2 The number of days is 28 程序运行示例4如下: Input year,month:1983,13 Input year,month:1983,-1 Input year,month:1983,1 The number of days is 31
时间: 2024-01-22 16:21:05 浏览: 68
编程实现从键盘上输入年份和月份,即可输出该页对应的天数含(闰年
以下是 Python 代码实现:
```python
year, month = input("Input year,month:").split(',')
year, month = int(year), int(month)
if month < 1 or month > 12:
print("Invalid month!")
else:
if month == 2:
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
days = 29
else:
days = 28
elif month in [4, 6, 9, 11]:
days = 30
else:
days = 31
print("The number of days is %d" % days)
```
运行示例:
```
Input year,month:1984,2
The number of days is 29
Input year,month:2000,2
The number of days is 29
Input year,month:1985,2
The number of days is 28
Input year,month:1983,13
Invalid month!
Input year,month:1983,-1
Invalid month!
Input year,month:1983,1
The number of days is 31
```
阅读全文