how to calculate the number of days in leap year
编写一个程序,提示用户输入一个月和一年,然后将消息输出到说明给定月份中有多少天(注意:如果用户进入二月)
我在第一个int和input中输入一个月和一年if (year/400 == 0) and (year/100 != 0) or (year/4 == 0):
号
1
2
3
4
5
6
7
8
9
10 print("this year is a leap year:")
if ( month =="December" or month =="January" or month =="March" or month =="May" or month =="July" or month =="August" or month =="October" ):
print ("days are: 31")
elif ( month =="April" or month =="June" or month =="September" or month =="November"):
print ("days are: 30")
elif ( month =="February" ):
print ("days are: 29")else:
号
1
2
3
4
5
6
7
8
9
10 print("this year is not a leap year:")
if ( month =="December" or month =="January" or month =="March" or month =="May" or month =="July" or month =="August" or month =="October" ):
print ("days are: 31")
elif ( month =="April" or month =="June" or month =="September" or month =="November"):
print ("days are: 30")
elif ( month =="February" ):
print ("days are: 28")号
代码不能正确地工作,它只能与其他代码一起工作,因此如果有人知道错误我认为第一种情况下的错误(如果年份/400=0)……
您可以使用内置模块日历中的isleap()函数。
1
2 import calendar
print (calendar.isleap(1900))打印
False ,因为1900不是闰年。使用这样的助手函数:
1
2
3
4
5
6
7
8
9
10
11
12 def is_leap(n):
if n % 400 == 0:
return True
if n % 100 == 0:
return False
if n % 4 == 0:
return True
else:
return False
if is_leap(year):
#your code号
看看年份是否可以被4整除。如果是这样,那就是闰年。
1
2 if not year % 4:
leap_year = True