Reputation: 179
New to Python, and I'm writing a program that has the user enter the month (in terms of a number, NOT the word - e.g. "3" not "March"), and the year (as in "2014"). I want the program to display the number of days in the month entered and the year. So if the user enters month 3 and the year 2005, it should display:
March 2005 has 31 days.
Here's my code:
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()
The problem is that instead of getting the proper format, I'm getting something like:
3 2005 has None days.
Please help! I greatly appreciate it.
Upvotes: 0
Views: 8328
Reputation: 766
################################################################
# 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()
Upvotes: 2
Reputation: 37319
I don't disagree with YS-L's answer. I'm submitting one to show with better formatting what I mean.
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()
This has obvious problems - for example, the leap year adjustment never happens. But it demonstrates how to capture return values from functions, which I think is the main point you're struggling with.
As a more advanced topic, here's how I'd do this using the datetime
built in library:
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")
Upvotes: 0
Reputation: 14738
Inside the function numberOfDays()
, you need to return the number of days, instead of just printing it. As it returns nothing, it prints None
in main()
. I.e. You need to have this line in numberOfDays()
:
return num_days
where num_days
is set as the number of days in the given month. In fact numberOfDays()
shouldn't event print anything at all. Just return the number of days is all it needs to do.
Also, why do you need to call so many enteredMonth()
inside numberOfDays()
? You can simply check the input argument month
.
Inside main()
, enteredMonth()
and enteredYear()
will not set any variable. You need to do something like month = enteredMonth()
.
Upvotes: 1