Question
It is python. I almost finished the code but I have an error. I have included all the tasks for each function even though it
It is python. I almost finished the code but I have an error. I have included all the tasks for each function even though it is done and working. I commented on where I need help fixing errors, It says,"NEED HELP HERE". The problem is with the last function, day_of_year()
Scenario # first function scenario. Your task is to write and test a function which takes one argument (a year) and returns True if the year is a leap year, or False otherwise. Note: we've also prepared a short testing code, which you can use to test your function. The code uses two lists - one with the test data, and the other containing the expected results. The code will tell you if any of your results are invalid. Scenario # second function scenerio Your task is to write and test a function which takes two arguments (a year and a month) and returns the number of days for the given month/year pair (while only February is sensitive to the year value, your function should be universal).
Scenario # third scenerio function. Your task is to write and test a function which takes three arguments (a year, a month, and a day of the month) and returns the corresponding day of the year, or returns None if any of the arguments is invalid. Use the previously written and tested functions. Add some test cases to the code. This test is only a beginning.
#Entire code
def is_year_leap(year): #first fucntion
if year % 4 != 0:
return False
elif year % 100 != 0:
return True
elif year % 400 != 0:
return False
else:
return True
def days_in_month(year, month): #second function
if month in [1, 3, 5, 7, 8, 10, 12]:
return 31
elif month in [4, 6, 9, 11]:
return 30
elif month == 2:
if is_year_leap(year):
return 29
else:
return 28
else:
return None
test_years = [1900, 2000, 2016, 1987]
test_months = [2, 2, 1, 11]
test_results = [28, 29, 31, 30]
for i in range(len(test_years)):
yr = test_years[i]
mo = test_months[i]
print(yr, mo, "->", end="")
result = days_in_month(yr, mo)
if result == test_results[i]:
print("OK")
else:
print("Failed")
def day_of_year(year, month, day): #Third function. NEED HELP HERE
days = 0
for m in range(1, month):
md = days_in_month(year, m)
if md == None:
return None
days += md
md = days_in_month(year, month)
if day >= 1 and day <= md:
return days + day
else:
return None
print(day_of_year(2000, 12, 31))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started