【问题标题】:Printing the number of days in a given month and year [Python]打印给定月份和年份的天数 [Python]
【发布时间】:2013-08-20 01:41:59
【问题描述】:

我一直在尝试找出一种方法来完成标题中的内容,而不使用从 Python 导入的任何日历/日期时间库。顶部几乎没有用于检查年份是否为闰年的功能,我希望在打印给定二月的天数时能够参考,但我不太确定如何这样做。 (我猜到了输出之类的东西。bla bla)

到目前为止,我已经想出了类似的东西,这应该可以清楚地说明我想要做什么,但是我对 Python 还是有点陌生​​,所以我希望得到一些关于修复我的代码的提示/帮助任务。

# A function to determine if a year is a leap year.
# Do not change this function.
def is_leap_year (year):
    return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)

# You should complete the definition of this function:

 def days_in_month(month, year):

    if month == 'September' or month == 'April' or month == 'June' or month == 'November'
    print 30

    elseif month == 'January' or month == 'March' or month == 'May' or month== 'July' or month == 'August' or month == 'October'\
    or month== 'December'
     print 31

    elseif month == 'February' and output.is_leap_year = True
    print 29

    elseif month == 'February' and output.is_leap_year = False
    print 28

    else print 'Blank'

好的,我已经修复了我的代码,它似乎每个月都输出正确的数据,但 2 月:

# A function to determine if a year is a leap year.
# Do not change this function.
def is_leap_year (year):
    return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)

# You should complete the definition of this function:

def days_in_month(month, year):

    if month in ['September', 'April', 'June', 'November']:
        print 30

    elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
        print 31        

    elif month == 'February' and is_leap_year == True:
        print 29

    elif month == 'February' and is_leap_year == False:
        print 28

有什么提示可以修复二月份的输出吗?

编辑:只需要在引用第一个函数时添加参数年份。以下是 100% 工作代码供将来参考:

# A function to determine if a year is a leap year.
# Do not change this function.
def is_leap_year(year):
    return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)

# You should complete the definition of this function:

def days_in_month(month, year):

    if month in ['September', 'April', 'June', 'November']:
        print 30

    elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
        print 31        

    elif month == 'February' and is_leap_year(year) == True:
        print 29

    elif month == 'February' and is_leap_year(year) == False:
        print 28

    else:
        return None

​

​

​

【问题讨论】:

  • elseif 应该是elif
  • Righto,解决了这个问题。此外,这是我在运行代码时遇到的错误代码,但我猜一旦对这个进行排序,还会有更多错误代码:def days_in_month(month, year): ^ IndentationError: unindent does not match any external缩进级别 .... ^ 应该在冒号上,只是不确定如何格式化此注释。
  • 你可以通过写if month in ('September', 'April', 'June', 'November'):来缩短它,因为它在一个函数中,你可能想要return计算的值而不是print它。
  • 我把它缩短了一点并修复了一些东西,介意在 O.P 中检查我的第二批代码吗?似乎无法让二月份的分析发挥作用..
  • 函数is_leap_year需要参数year,你错过了

标签: python python-2.7 calendar days


【解决方案1】:

更 Python 的方法是在字典中定义映射,然后简单地从字典中检索值。

试试看:

days_in_month_dict = {"January": 31, "February": 28, 
                      "March": 31, "April": 30,
                      "May": 31, "June": 30, 
                      "July": 31, "August": 31,
                      "September": 30, "October": 31,
                      "November": 30, "December": 31}

def is_leap_year(year):
    return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)

def days_in_month(year, month):
    if is_leap_year(year) and month == "February":
        return 28

    try: 
        #attempt to get value from dictionary 
        return days_in_month_dict[month]
    except KeyError:
        #key does not exist, so we caught the error
        return None

【讨论】:

    【解决方案2】:

    您的代码中有一些语法错误:

    1. def days_in_month(month,year) 之前不应有空格。 Python 使用缩进来分隔代码块。这是您在评论中给出的错误。
    2. python中没有elseif,应该是elif
    3. output.is_leap_year = True,应该是is_leap_year(year) == True。 False 部分也应该更改。
    4. 在if 声明和else 之后应该有一个:,就像

      if month == 'September' or month == 'April' or month == 'June' or month == 'November':
          print 30
      elif month == 'January' or month == 'March' or month == 'May' or month== 'July' or month == 'August' or month == 'October' or month== 'December':
          print 31
      elif month == 'February' and is_leap_year(year) == True:
          print 29
      elif month == 'February' and is_leap_year(year) == False:
          print 28
      else:
          print 'Blank'
      

    【讨论】:

    • 好的,我已经解决了所有这些问题,感谢您的帮助。现在我在第 10 行返回一个语法错误,在 'November' 字符串右侧的引号中,这可能是什么原因造成的?
    【解决方案3】:
    """
    Takes the year and month as input and returns the no. of days
    """
    def is_leap_year (year):
        return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
    
    def days_in_month(month, year):
        if month == 'September' or month == 'April' or month == 'June' or month == 'November':
            result=30
        elif month == 'January' or month == 'March' or month == 'May' or month== 'July' or month == 'August' or month == 'October'or month== 'December':
            result=31
    
        elif month == 'February' and output.is_leap_year ==True:
            result=29
    
        elif month == 'February' and output.is_leap_year == False:
            result=28
        return result
    
    print(is_leap_year(2016))
    print(days_in_month('September',2016))
    

    【讨论】:

      【解决方案4】:
      month = int (input ('month (1-12): '))
      
      if month < 13:
          if month == 2:
              year = int (input ('year: '))
              if year % 4 == 0:
                  if year % 100 == 0:
                      if year % 400 == 0:
                          print ('29')
                      else:
                          print ('28')
                  else:
                      print ('29')
              else:
                  print ('28')
      
          elif month >= 8:
              if month % 2 == 0:
                  print ('31')
              else:
                  print ('30')
      
          elif month % 2 == 0:
              print ('30')
          else:
              print ('31')
      
      else:
          print ('Only 1-12 accepted')
      

      【讨论】:

      • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的答案以添加解释并说明适用的限制和假设。 From Review
      【解决方案5】:
      month=input("month")
      year=int(input("year"))
      if year%4==0:
              year=('leap year')
      if month in ['September', 'April', 'June', 'November']:
              print ("30")
      
      elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
              print ("31")        
      
      elif month == 'February' and year == "leap year":
              print ("29")
      
      elif month == 'February' and  year != "leap year":
              print ("28")
      
      else:
          print("none")
      

      【讨论】:

      • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      相关资源
      最近更新 更多