闰年是366天,平年是365天,闰年的2月是29天。平年的2月是28天。判断某一年是否为闰年,需要满足其中一个条件:年份被4整除且不被100整除,或者被100整除且被400整除。例如:2004,或2000就是闰年,
而1900不是闰年,是平年。
第一种方法:
y=int(input("输入一个年份:"))
#第一种方法,用到and,or, 这两个优先运行 and
if y%4==0 and y%100!=0 or y%100==0 and y%400==0:
print(f'{y}年是闰年。')
else:
print(f'{y}年是平年。')
第二种方法:
#第二种方法,用if嵌套
y=int(input("输入一个年份:"))
if y%4==0:
if y%100!=0:
print(f'{y}年是闰年。')
else:#这个else表示:y % 100 == 0
if y % 400 == 0:
print(f'{y}年是闰年。')
else:#这个else表示:if y % 400 != 0:
print(f'{y}年是平年。')