输入年份.月份,输出该月份天数。
时间: 2024-04-29 09:23:38 浏览: 6
以下是Python的代码实现:
```
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
# 判断是否为闰年
is_leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
# 计算天数
if month in [1, 3, 5, 7, 8, 10, 12]:
days = 31
elif month in [4, 6, 9, 11]:
days = 30
else:
days = 29 if is_leap else 28
print(f"{year}年{month}月有{days}天")
```
输入示例:
```
请输入年份:2021
请输入月份:2
```
输出示例:
```
2021年2月有28天
```
相关问题
输入年份和月份,输出该月份天数
我可以回答这个问题。不同的月份有不同的天数,一般来说,1月、3月、5月、7月、8月、10月和12月有31天,4月、6月、9月和11月有30天,而2月份的天数则根据是否为闰年而有所不同。如果是闰年,2月份有29天,否则有28天。
输入年份和月份,输出该月天数
这是一个常见的编程问题,可以使用以下代码来解决:
```python
import calendar
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
days = calendar.monthrange(year, month)[1]
print(f"{year}年{month}月有{days}天。")
```
这个程序使用了 Python 的 calendar 模块,其中的 monthrange 函数可以返回指定年份和月份的第一天是星期几以及该月的天数。我们只需要取出第二个返回值即可得到该月的天数。