定义函数,用于判断用户输入的年份是不是闰年,如果是输出XXXX年是闰年,否则输出不是闰年,几年后是闰年
时间: 2024-03-01 17:50:22 浏览: 70
好的,下面是实现该功能的 Python 代码:
```python
def is_leap_year(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True
else:
return False
if __name__ == '__main__':
year = int(input("请输入年份:"))
if is_leap_year(year):
print(f"{year}年是闰年")
else:
print(f"{year}年不是闰年")
for i in range(1, 100):
if is_leap_year(year + i):
print(f"{year+i}年是闰年")
break
```
在上述代码中,我们定义了一个 `is_leap_year` 函数,用于判断输入的年份 `year` 是不是闰年。如果是闰年,该函数返回 True,否则返回 False。
然后,在 `main` 函数中,我们使用 `input` 函数获取用户输入的年份。如果该年份是闰年,我们输出提示信息 “XXXX年是闰年”,否则输出 “不是闰年”。接着,我们使用一个循环判断该年份后的 100 年中,哪一年是闰年,并输出提示信息。
注意:为了保证代码的可重用性,我们将判断是否为闰年的函数 `is_leap_year` 定义为一个独立的函数,而不是直接在 `main` 函数中实现。
阅读全文
相关推荐













