类型:简单分支
描述
写一个程序用于判断用户输入的年份是不是闰年,如果是输出“True”,如果不是输出“False”。
输入格式
输入一个代表年份的正整数
输出格式
“True”或“False”
示例 1
输入:1900
输出:False
示例 2
输入:2000
输出:True
参考代码
# 基本原理
year = int(input())
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print(True)
else:
print(False)
# 使用条件表达式的话:
year = int(input())
print(True) if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0) else print(False)
# 使用函数的话:
def leap(year):
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
# 能被400整除是闰年;能被4整除且同时不能被100整除的是闰年
return True
else:
return False
print(leap(int(input())))
# 使用函数还可以更简单,直接将条件作为逻辑表达式返回
def leap(year):
return year % 400 == 0 or year % 4 == 0 and year % 100 != 0
print(leap(int(input())))