Python的新手,我正在编写一个用户输入月份的程序(以数字而非单词 - 例如“3”而不是“March”)和年份(如“2014”) “)。我希望程序显示输入月份和年份的天数。因此,如果用户输入第3个月和2005年,则应显示:
2005年3月有31天。
这是我的代码:
def enteredMonth():
month = int(input("Enter a month in terms of a number: "))
return month
def enteredYear():
year = int(input("Enter a year: "))
return int(year)
def leapYear(year):
if year % 4 == 0:
return True
else:
return False
def numberOfDays(month):
if enteredMonth() == 1:
month = "January"
print ("31")
elif enteredMonth() == 2:
month = "February"
print ("28")
elif enteredMonth() == 2 and leapYear() == true:
month = "February"
print ("29")
elif enteredMonth() == 3:
month = "March"
print ("31")
elif enteredMonth() == 4:
month = "April"
print ("30")
elif enteredMonth() == 5:
month = "May"
print ("31")
elif enteredMonth() == 6:
month = "June"
print ("30")
elif enteredMonth() == 7:
month = "July"
print ("31")
elif enteredMonth() == 8:
month = "August"
print ("31")
elif enteredMonth() == 9:
month = "September"
print ("30")
elif enteredMonth() == 10:
month = "October"
print ("31")
elif enteredMonth() == 11:
month = "November"
print ("30")
elif enteredMonth() == 12:
month = "December"
print ("31")
else:
print("Please enter a valid month")
def main():
enteredMonth()
enteredYear()
leapYear(year)
numberOfDays(month)
print(month, enteredYear(), "has", numberOfDays(month) , "days")
if __name__ == '__main__':
main()
问题在于,我没有获得正确的格式,而是获得了类似的内容:
2005年没有天。
请帮忙!我非常感激。
答案 0 :(得分:2)
################################################################
# OR Another way to achieve same thing
def numberOfDays(month, year):
daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if month > len(daysInMonths) or year < 0 or month < 0:
return "Please enter a valid month/year"
# Here, only divisible by 4 as leap-year but there are many
# more conditions for a leap year which you can add in expression
return daysInMonths[month-1] + int((year % 4) == 0 and month == 2)
def main():
year = int(input("Enter a year: "))
month = int(input("Enter a month in terms of a number: "))
print(month, year, "has", numberOfDays(month, year) , "days")
if __name__ == '__main__':
main()
################################################################
# OR using standard library
from calendar import monthrange
def main():
year = int(input("Enter a year: "))
month = int(input("Enter a month in terms of a number: "))
if year < 0 or month < 0:
print "Pleae enter a valid month/year"
return
print(month, year, "has", monthrange(year, month)[1] , "days")
if __name__ == '__main__':
main()
答案 1 :(得分:1)
在函数numberOfDays()
内,您需要返回天数,而不是仅仅打印它。由于它不返回任何内容,因此会在None
中打印main()
。即您需要在numberOfDays()
中包含此行:
return num_days
其中num_days
设置为给定月份的天数。事实上,numberOfDays()
不应该打印任何东西。 只需即可返回所需的天数。
另外,为什么你需要在enteredMonth()
内拨打这么多numberOfDays()
?您只需检查输入参数month
。
在main()
内,enteredMonth()
和enteredYear()
不会设置任何变量。您需要执行month = enteredMonth()
。
答案 2 :(得分:0)
我不同意YS-L的回答。我正在提交一个以更好的格式显示我的意思。
def enteredMonth():
month = int(input("Enter a month in terms of a number: "))
return month
def enteredYear():
year = int(input("Enter a year: "))
return int(year)
def leapYear(year):
if year % 4 == 0:
return True
else:
return False
def numberOfDays(month):
if month == 1:
return 31
elif month == 2:
return 28
# etc. Actually, the Pythonic implementation would be to use a dict
# that maps month numbers to their usual number of days.
def main():
month = enteredMonth()
year = enteredYear()
is_leapyear = leapYear(year)
number_of_days = numberOfDays(month)
print(month, year, "has", number_of_days, "days")
if __name__ == '__main__':
main()
这有明显的问题 - 例如,闰年调整从未发生过。但它演示了如何从函数中捕获返回值,我认为这是你正在努力的主要观点。
作为一个更高级的主题,以下是我使用datetime
内置库执行此操作的方法:
import datetime
def enteredMonth():
month = int(input("Enter a month in terms of a number: "))
return month
def enteredYear():
year = int(input("Enter a year: "))
return year
def numberOfDays(year, month):
first_of_entered_month = datetime.date(year, month, 1)
someday_next_month = first_of_entered_month + datetime.timedelta(days=31)
first_of_next_month = someday_next_month.replace(day=1)
last_of_entered_month = first_of_next_month - datetime.timedelta(days=1)
return last_of_entered_month.day
def main():
month = enteredMonth()
year = enteredYear()
number_of_days = numberOfDays(year, month)
pretty_month_year = datetime.date(year, month, 1).strftime('%B %Y')
print(pretty_month_year, "has", number_of_days, "days")