我最近在這個網站上註冊,因爲我真的被困在需要創建程序的地方,用戶輸入一個月,程序顯示有多少天那個月。用戶還必須輸入年份,以便程序可以檢查是否閏年,以防用戶輸入二月份。日曆程序幫助(字符串變量)
這是編碼,我拿出這麼遠:
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編程的許多具體細節,所以如果任何人可以幫助我在正確的方向,將不勝感激!
你有使用Python [日曆模塊(http://docs.python.org/library/calendar考慮。 HTML)從Python [標準庫](http://docs.python.org/library/index.html)? – srgerg
你非常接近。有幾點提示:'get_days'需要兩個參數,因爲你需要月份和年份(測試閏年)。另外,'leap_year'是一個函數,所以你必須調用它並傳遞一個參數。另外,你的'if'陳述有點不吉利。回顧一下,確保他們按照你的期望行事。最後,它應該是「真」和「假」,而不是「真」和「假」。 – senderle
此外,我不確定你的算法來確定閏年是相當準確/完整的。來自Wikipeadia:「可以被100整除的年份不是閏年,除非它們也可以被400整除,在這種情況下,它們是閏年」http://en.wikipedia.org/wiki/Leap_year – Levon