输入一个年份,用函数判断其是否为闰年
时间: 2024-05-07 13:17:36 浏览: 95
好的,我可以帮您编写一个Python函数,用于判断输入的年份是否为闰年。
以下是示例代码:
```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,表示要判断的年份。
函数内部通过数学计算来判断是否为闰年,逐层判断年份是否能被 4 整除,是否能被 100 整除,以及是否能被 400 整除。
如果该年份是闰年,则返回 True,否则返回 False。
您可以在程序中直接调用该函数,传入需要判断的年份作为参数,来得到判断结果。例如:
```python
year = 2024
if is_leap_year(year):
print(year, "是闰年")
else:
print(year, "不是闰年")
```
输出结果为:“2024 是闰年”。
相关问题
输入一个年份,使用函数判断其是否为闰年
可以使用以下函数来判断一个年份是否为闰年:
```python
def is_leap_year(year):
"""
判断是否为闰年
"""
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True
else:
return False
```
函数接收一个年份作为参数,如果是闰年则返回 True,否则返回 False。其中判断闰年的方法是:能被4整除但不能被100整除的年份为普通闰年,能被400整除的为世纪闰年。
python输入一个年份,使用函数判断其是否为闰年
好的,我了解了你的要求,我可以为你提供以下 Python 代码,来判断一个年份是否为闰年:
```python
def is_leap_year(year):
"""
判断是否为闰年
"""
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
```
你可以调用这个函数,并传入一个年份作为参数,如果返回 True,那么这个年份就是闰年,否则就不是闰年。
阅读全文
相关推荐













