if、else的练习记录——判断月份天数

 1 m = int(input("请输入月份"))
 2 if m in (1,3,5,7,8,10,12):
 3     print("每年的"+str(m)+"月都是31天")
 4 else:
 5     if m in (4,6,9,11):
 6         print("每年的"+str(m)+"月都是30天")
 7     else:
 8         y = int(input("请输入年份"))
 9         if y%400 == 0:
10             print(str(y)+"年的"+str(m)+"月有29天")
11         else:
12             if y%4 == 0 and y%100 != 0:
13                 print(str(y)+"年的"+str(m)+"月有29天")
14             else:
15                 print(str(y)+"年的"+str(m)+"月有28天")

如果输入的月份为2月,则要求输入年份并判断是否为闰年,返回28或29天

闰年分两种情况:

  1、可以被4整除,但不能被100整除

  2、可以被400整除

猜你喜欢

转载自www.cnblogs.com/penn666/p/12054051.html