This question is from a python institute question titled ‘How many days: writing and using your own functions’
The goal is to write a function that returns number of days in any given month of a year (leap or otherwise).
def is_leap_year(year):
if year % 4 == 0:
return True
else:
return False
def days_in_month(year, month):
month_day = [31,28,31,30,31,30,31,31,30,31,30,31]
if month > 12 or month < 1:
return None , "Please choose a number between and including 1 and 12"
if is_leap_year(False):
return month_day[month - 1]
if is_leap_year(True) and month == 2:
month_day[1] == 29
return month_day[month - 1]
print(is_leap_year(2024))
print(days_in_month(2024,2))
The above produces:
True
None
2024 is a leap year and therefore returns True – but it does not return 29 days of February. I have played around the the indentation and it produces also True, 28 (still not 29).
Apologies if this is a simple question I am new to python.
Idraq Siddiqui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.