用py写一个闰年计算器
以下是用Python编写的闰年计算器程序:
# 定义函数 is_leap_year 判断是否为闰年
def is_leap_year(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True
else:
return False
# 输入年份
year = int(input("请输入年份:"))
# 调用函数判断是否为闰年
if is_leap_year(year):
print(year, "是闰年")
else:
print(year, "不是闰年")
运行结果:
请输入年份:2000
2000 是闰年
说明:
- 定义函数
is_leap_year(year),判断是否为闰年。如果是闰年,返回True,否则返回False。 - 输入年份,使用
int()函数将字符串转换为整数。 - 调用函数
is_leap_year(year)判断该年份是否为闰年,如果是,输出结果为“年份是闰年”,否则输出结果为“年份不是闰年”。
原文地址: http://www.cveoy.top/t/topic/ba5Q 著作权归作者所有。请勿转载和采集!