用python语言实现输入年份和月份,输出该月的天数
时间: 2023-05-30 14:03:54 浏览: 205
以下是Python代码实现:
```
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
# 计算天数
if month in [1, 3, 5, 7, 8, 10, 12]:
days = 31
elif month in [4, 6, 9, 11]:
days = 30
elif month == 2:
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
days = 29
else:
days = 28
else:
print("输入的月份有误!")
# 输出结果
if month in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
print("{}年0{}月有{}天".format(year, month, days))
else:
print("{}年{}月有{}天".format(year, month, days))
```
运行程序后,会提示输入年份和月份,然后输出该月的天数。其中,计算天数的部分使用了if语句和逻辑运算符,判断该月份的天数是31天、30天还是28/29天。最后,根据月份的位数输出对应的结果。
相关问题
输入年份及月份输出这一年的这个月的天数
可以使用Python编程语言来实现这个功能,具体代码如下:
```python
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
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 == and year % 100 != ) or year % 400 == :
days = 29
else:
days = 28
print("{}年{}月有{}天".format(year, month, days))
```
运行程序后,输入年份和月份,即可输出该年该月的天数。其中,通过判断月份的值,来确定该月的天数。对于2月份,需要判断是否为闰年,如果是闰年则有29天,否则有28天。最后,使用字符串格式化输出结果。
用python语言 输入一个年份,再输入一个月份,判断其是平年还是闰年,然后输出当前月份的天数
以下是用 Python 语言实现判断年份是否为闰年并输出当前月份天数的代码:
```python
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
leap_year = True
else:
leap_year = False
if month in [1, 3, 5, 7, 8, 10, 12]:
days = 31
elif month in [4, 6, 9, 11]:
days = 30
else:
if leap_year:
days = 29
else:
days = 28
if leap_year:
print("{}年是闰年".format(year))
else:
print("{}年是平年".format(year))
print("{}月份有{}天".format(month, days))
```
如果输入的年份是闰年,则输出结果为:
```
请输入年份:2020
请输入月份:2
2020年是闰年
2月份有29天
```
如果输入的年份是平年,则输出结果为:
```
请输入年份:2021
请输入月份:2
2021年是平年
2月份有28天
```