2012-05-08 20 views
0

我最近在這個網站上註冊,因爲我真的被困在需要創建程序的地方,用戶輸入一個月,程序顯示有多少天那個月。用戶還必須輸入年份,以便程序可以檢查是否閏年,以防用戶輸入二月份。日曆程序幫助(字符串變量)

這是編碼,我拿出這麼遠:

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誤差函數它只是我不知道 如何編寫代碼,以便該程序知道用戶輸入一個月,如1月或3月的 。我猜測輸入必須是一個變量,並且由於每個 月是一個字符串,所以需要一個變量字符串。但我對此可能完全錯誤。我很新的python(正好2周現在的關閉和編程學校工作),所以我不太確定python編程的許多具體細節,所以如果任何人可以幫助我在正確的方向,將不勝感激!

+1

你有使用Python [日曆模塊(http://docs.python.org/library/calendar考慮。 HTML)從Python [標準庫](http://docs.python.org/library/index.html)? – srgerg

+0

你非常接近。有幾點提示:'get_days'需要兩個參數,因爲你需要月份和年份(測試閏年)。另外,'leap_year'是一個函數,所以你必須調用它並傳遞一個參數。另外,你的'if'陳述有點不吉利。回顧一下,確保他們按照你的期望行事。最後,它應該是「真」和「假」,而不是「真」和「假」。 – senderle

+0

此外,我不確定你的算法來確定閏年是相當準確/完整的。來自Wikipeadia:「可以被100整除的年份不是閏年,除非它們也可以被400整除,在這種情況下,它們是閏年」http://en.wikipedia.org/wiki/Leap_year – Levon

回答

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() 
+0

甚至沒有考慮過leap_status變量或leap_year傳球。非常感謝我讓程序工作:) – Jertise

+0

請記住**通過點擊答案左側的勾號圖標來接受滿足您的答案。獎勵是這個網站的力量。不要讓任何人提醒你:-) – aitchnyu

0

if-elif在2月份被打破。小的變化是繼續不間斷的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' 
+0

之前,需要將字符串中的年份更改爲數字,現在我閱讀了您的評論,這使得它變得更加有意義,我真的不敢相信我沒有這樣想過,但是非常感謝! – Jertise

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!' 
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 
+1

有關此代碼的一些解釋只回答可能會有用。 – Pyves

+0

它通過接受一個月中的某天以及該月的開始日期來打印日曆。它是有用的,每年 –

+0

是我的代碼有用 –

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