date - Converting the number of days in a month in Python? -
new python, , i'm writing program has user enter month (in terms of number, not word - e.g. "3" not "march"), , year (as in "2014"). want program display number of days in month entered , year. if user enters month 3 , year 2005, should display:
march 2005 has 31 days.
here's code:
def enteredmonth(): month = int(input("enter month in terms of number: ")) return month def enteredyear(): year = int(input("enter 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 , 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 valid month") def main(): enteredmonth() enteredyear() leapyear(year) numberofdays(month) print(month, enteredyear(), "has", numberofdays(month) , "days") if __name__ == '__main__': main()
the problem instead of getting proper format, i'm getting like:
3 2005 has none days.
please help! appreciate it.
i don't disagree ys-l's answer. i'm submitting 1 show better formatting mean.
def enteredmonth(): month = int(input("enter month in terms of number: ")) return month def enteredyear(): year = int(input("enter 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, pythonic implementation use dict # maps month numbers 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()
this has obvious problems - example, leap year adjustment never happens. demonstrates how capture return values functions, think main point you're struggling with.
as more advanced topic, here's how i'd using datetime
built in library:
import datetime def enteredmonth(): month = int(input("enter month in terms of number: ")) return month def enteredyear(): year = int(input("enter 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")
Comments
Post a Comment