编写python程序,接收用户输入的年份和月份,输出该月天数。要求使用if语句实现。
时间: 2024-06-06 21:11:04 浏览: 19
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
if month == 2:
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print("该月天数为29天")
else:
print("该月天数为28天")
elif month == 4 or month == 6 or month == 9 or month == 11:
print("该月天数为30天")
else:
print("该月天数为31天")
相关问题
用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
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 == 0 and year % 100 != 0) or year % 400 == 0:
days = 29
else:
days = 28
print(f"{year}年{month}月有{days}天")
```
首先,我们通过 `input` 函数获取用户输入的年份和月份,然后使用 `if` 条件语句判断该月份的天数。对于大月份(1、3、5、7、8、10、12月),天数为31天;对于小月份(4、6、9、11月),天数为30天;对于2月份,需要根据是否是闰年来判断天数。如果该年是闰年,则2月份有29天,否则有28天。最后,使用 `print` 函数输出结果。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)