0

我最近在这个网站上注册了,因为我真的被困在任务中,我需要创建一个程序,用户输入一个月,程序会显示那个月有多少天。用户还必须输入年份,以便程序可以检查它是否是闰年,以防用户输入 2 月份。

这是我到目前为止提出的编码:

    def get_year():
    year = raw_input("Please enter the year: ")
    return year

def get_month():
    month = raw_input("Please enter the month: ")
    return month

def leap_year(year):
    if year % 4 == 0:
        return true
    else:
        return false

def get_days(month):
    if month == "january":
        print "31 days"
    elif month == "february" and leap_year == true:
        print "29 days"
    else:
        print "28 days"
    if month == "march":
        print "31 days"
    elif month == "april":
        print "30 days"
    elif month == "may":
        print "31 days"
    elif month == "june":
        print "30 days"
    elif month == "july":
        print "31 days"
    elif month == "august":
        print "31 days"
    elif month == "september":
        print "30 days"
    elif month == "october":
        print "31 days"
    elif month == "november":
        print "30 days"
    elif month == "december":
        print "31 days"
    else:
        print

def main():
    user_year = get_year()
    user_month = get_month()
    leap_year(user_year)
    get_days(user_month)

main()

无论如何,很明显我的 get_days 函数中有一个错误,只是我不确定如何编写代码,以便程序知道用户输入了一个月,例如一月或三月。我猜输入必须是一个变量,并且由于每个月都是一个字符串,因此需要一个变量字符串。但我可能完全错了。

我对 python 很陌生(现在正好 2 周的假期和学校工作的编程)所以我不太确定 python 编程的许多细节,所以如果有人可以在正确的方向上帮助我,那就是非常感激!

4

5 回答 5

0

if-elif 在二月份被打破。细微的变化是继续使用 if-elif 逻辑的不间断模式:

def get_days(month):
    if month == "january":
        print "31 days"
    elif month == "february" and leap_year:
        print "29 days"
    elif month == "february" and not leap_year:
        print "28 days"
    elif month == "march":
        print "31 days"
    elif month == "april":
        print "30 days"
    elif month == "may":
        print "31 days"
    elif month == "june":
        print "30 days"
    elif month == "july":
        print "31 days"
    elif month == "august":
        print "31 days"
    elif month == "september":
        print "30 days"
    elif month == "october":
        print "31 days"
    elif month == "november":
        print "30 days"
    elif month == "december":
        print "31 days"
    else:
        print 'unknown month'
于 2012-05-08T05:10:14.353 回答
0

你有点接近。我做了一些我评论的更改。正如 Raymond Hettinger 指出的那样,你get_days(month)完全崩溃了

def get_year():
    year = raw_input("Please enter the year: ")
    return int(year) #otherwise it is a string!

def get_month():
    month = raw_input("Please enter the month: ")
    return month

def leap_year(year):
    if year % 4 == 0:
        return True
    else:
        return False

def get_days(month,leap_year): #leap_year must be passes to this function
    #This checks for "january" and "february" with leap years
    #and falls back to last option on EVERYTHING ELSE like a feb without a leap year or even a "march"
    if month == "january":
        print "31 days"
    elif month == "february" and leap_year == True:
        print "29 days"
    else:
        print "28 days"

    #this is a separate block that runs AFTER the previous block
    if month == "march":
        print "31 days"
    elif month == "april":
        print "30 days"
    elif month == "may":
        print "31 days"
    elif month == "june":
        print "30 days"
    elif month == "july":
        print "31 days"
    elif month == "august":
        print "31 days"
    elif month == "september":
        print "30 days"
    elif month == "october":
        print "31 days"
    elif month == "november":
        print "30 days"
    elif month == "december":
        print "31 days"
    else:
        print "invalid input" #so that it doesnt fail silently when I enter 2

def main():
    user_year = get_year()
    user_month = get_month()
    leap_status = leap_year(user_year) #store the leap_year status to a variable
    get_days(user_month, leap_status)  #and then pass it to a function

main()
于 2012-05-08T05:25:18.797 回答
0

我建议您使用字典,它是 python 中的内置数据类型。

def get_days(year, month):
    """
    take year and month ,return the days in that month.
    """
    #define a dictionary and the month is key which value is days 
    daydict = dict()    
    daydict = ['january'=31, 'february'=28, 'februaryinleapyear'=29,
                                        'march'=31, 'april'=30,
                                        'may'=31, 'june'=30,
                                        'july'=31, 'august'=31,
                                        'september'=30, 'october'=31,
                                        'november'=30, 'december'=31 ]

    try:
        if month in daydict:
            if month == 'february' and leap_year(year):     
                print daydict[februaryinleapyear] 
            else:
                print daydict[month]
        else:
            print 'The year or month you input is invalid !'
    except:
        print 'error!'
于 2012-05-08T05:39:34.260 回答
0
 n=int(input('enter no of days in a month= '))
 #0=sunday
 #1=moday
 #2=tuesday
 #3=wednesday
 #4=thursday
 #5=friday
 #6=saturday
 d=int(input('enter the starting day of month= '))
 print('sun','mon','tue','wed','thu','fri','sat',sep='\t')
 for j in range(d):
    print (' ',end='\t')
 i=1
 while(i<=n):
    print (i,end='\t')
    if(i+d)%7==0:
      print('\t')
    i=i+1
于 2017-07-19T09:29:00.780 回答
0

此代码要求用户输入(更简单的代码

#python calendar days in month.
month= input ("Enter the month('January', ...,'December'): ")   # ask for inputs from user 

start=input ("Enter the start day ('Monday', ..., 'Sunday'): ")  


if start== "Monday" :
    num=1
elif start== "Tuesday" :
    num=0

elif start== "Wednesday" :
    num=-1

elif start== "Thursday" :
    num=-2   

elif start== "Friday" :
    num=-3 

elif start== "Sartday" :
    num=-4
elif start=="Sunday":
    num=-5


print(month)    
print("Mo Tu We Th Fr Sa Su")  #the header of the Calender


if month== "January" or month=="March" or month=="May" or month=="July" or month=="August" or month=="October" or month=="December" :

  #for month with 31 days
    for num_ in range (num,num+41,7): 
        for i in range(7):
            if num<1:
                print ('   ',end="")
            elif num>31:
                print("",end="")
            else:
                print ("{0:>2}".format(num),end="")
                if i<6 and num<31:
                    print(" ",end="") 
            num +=1
        print() 


elif month== "April" or month=="June" or month=="Septemmber" or month=="November":
   #for month with 30 days      
    for num_ in range (num,num+41,7): 
        for i in range(7):
            if num<1:
                print ('   ',end="")
            elif num>30:
                print("",end="")
            else:
                print ("{0:>2}".format(num),end="")
                if i<6 and num<30:
                    print(" ",end="") 
            num +=1
        print() 


elif month== "February" :
  # february is an exception : it has 28 days 
    for num_ in range (num,num+41,7): 
        for i in range(7):
            if num<1:
                print ('   ',end="")
            elif num>28:
                print("",end="")
            else:

                print ("{0:>2}".format(num),end="")
                if i<6 and num<28:
                    print(" ",end="") 
            num +=1
        print() 


else:
    print("invalid entry")
于 2017-08-13T22:40:09.267 回答