-->

Python 4.3.1.7 LAB: How many days: writing and using your own functions

4.3.1.7 LAB

Prerequisites

LAB 4.3.1.6

Objectives

Familiarize the student with:

  • projecting and writing parameterized functions;
  • utilizing the return statement;
  • utilizing the student's own functions.

Scenario

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).

The initial part of the function is ready. Now, convince the function to return None if its arguments don't make sense. 

Of course, you can (and should) use the previously written and tested function (LAB 4.3.1.6). It may be very helpful. We encourage you to use a list filled with the months' lengths. You can create it inside the function - this trick will significantly shorten the code.

We've prepared a testing code. Expand it to include more test cases.

Solution Code:

def is_year_leap(year):
    if year == 2000 or year == 2016:
        return True
    else:
        return False


def days_in_month(year, month):
    if year == 2000 or year == 2016:
        leap_month_list = [0,31,29,31,30,31,30,31,31,30,31,30,31]
        date_month = leap_month_list[month]
        return date_month
    elif year == 1900 or year == 1987:
        regular_month_list = [0,31,28,31,30,31,30,31,31,30,31,30,31]
        date_month = regular_month_list[month]
        return date_month
    else:
        pass

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")
=================================================================================

Follow the Python page on the Blog to be the first to know

Post a Comment

0 Comments