日历程序帮助(字符串变量)

时间:2012-05-08 04:02:55

标签: python string function variables calendar

我最近在这个网站上注册了,因为我真的陷入了一项任务,我需要创建一个用户输入一个月的程序,程序会显示该月的天数。用户还必须输入年份,以便程序可以检查是否是闰年,以防用户输入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函数中有一个错误,我只是不确定 如何编写代码,以便程序知道用户输入一个月如 一月或三月。我猜测输入必须是一个变量,因为每个 month是一个字符串,需要一个变量字符串。但我可能完全错了。

我对python很新(现在正好两个星期以及关于学校作业的编程)所以我对python编程的许多细节都不太清楚,所以如果有人能帮助我正确的方向,非常感谢!

5 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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()

答案 2 :(得分: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!'

答案 3 :(得分: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

答案 4 :(得分:0)

此代码询问用户输入(more simple codes

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