计算闰年和平年的程序可以通过以下步骤实现:
输入年份 :首先,程序需要接收用户输入的年份。判断闰年条件
如果年份能被4整除但不能被100整除,则为闰年。
如果年份能被400整除,也是闰年。
其他情况均为平年。
输出结果:
根据判断结果,程序应输出该年份是闰年还是平年,并可以进一步输出该年份2月的天数(28天或29天)。
```python
def is_leap_year(year):
"""判断给定的年份是否为闰年"""
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
输入年份
year = int(input("请输入一个年份: "))
判断并输出结果
if is_leap_year(year):
print(f"{year}年是闰年,2月有29天。")
else:
print(f"{year}年是平年,2月有28天。")
```
解释
函数定义:
`is_leap_year`函数用于判断给定的年份是否为闰年。
闰年判断条件
`year % 4 == 0`:年份能被4整除。
`year % 100 == 0`:年份能被100整除。
`year % 400 == 0`:年份能被400整除。
输入输出
用户输入一个年份。
调用`is_leap_year`函数判断该年份是否为闰年。
根据判断结果输出相应的信息。
这个程序可以简单地帮助用户判断任意年份是否为闰年,并了解该年份2月的天数。