用if函数:输入年份月份输出天数
时间: 2023-05-25 15:05:56 浏览: 82
以下是 Python 代码:
```python
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
if month == 2:
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
days = 29
else:
days = 28
elif month == 4 or month == 6 or month == 9 or month == 11:
days = 30
else:
days = 31
print("{}年{}月有{}天".format(year,month,days))
```
示例输出:
输入:
```
请输入年份:2022
请输入月份:2
```
输出:
```
2022年2月有28天
```
相关问题
用if函数:输入年份月份输出天数带报错
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
if month in [1, 3, 5, 7, 8, 10, 12]:
print("{}年{}月共有31天。".format(year, month))
elif month in [4, 6, 9, 11]:
print("{}年{}月共有30天。".format(year, month))
elif month == 2:
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
print("{}年{}月共有29天。".format(year, month))
else:
print("{}年{}月共有28天。".format(year, month))
else:
print("输入的月份有误,请输入1-12之间的整数。")
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
elif month == 2 and leap_year:
days = 29
else:
days = 28
print(f"{year}年{month}月共有{days}天。")
```
在这个代码中,我们首先使用 `input()` 函数获取用户输入的年份和月份。然后我们使用 `if` 语句来判断输入的年份是否为闰年,如果是,则将变量 `leap_year` 设为 `True`,否则设为 `False`。接着我们再使用 `if` 语句来判断该月份的天数,分别处理每个月的情况。最后,我们使用 `print()` 函数输出结果。
相关推荐
![html](https://img-home.csdnimg.cn/images/20210720083451.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)