如何计算闰年的天数

时间:2014-11-16 02:36:36

标签: python

编写一个程序,提示用户输入一个月和一年,然后输出一条消息 说明给定月份的天数(注意:如果用户,您需要考虑闰年 进入2月份的月份)

我在第一个int和输入中使用输入一个月和一年

  
    

if(year / 400 == 0)and(year / 100!= 0)或(year / 4 == 0):

  
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")
  
    

否则:

  
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")

代码不正确,它只能与else一起使用,所以如果有人对错误有所了解 我认为第一个条件中的错误(如果年/ 400 == 0).......

3 个答案:

答案 0 :(得分:1)

您可以使用内置模块日历中的isleap()功能。

import calendar
print (calendar.isleap(1900))

打印False,因为1900年不是闰年。

答案 1 :(得分:0)

使用这样的辅助函数:

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

答案 2 :(得分:-2)

看看年份是否可被4整除。如果是这样,那就是闰年。

if not year % 4:
    leap_year = True