Start Your Journey with Linux Command Line
| writing and using your own functions |
LAB 4.3.1.6
Familiarize the student with:
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.| What is needed |
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.
Follow the Python page on the Blog to be the first to know ❤
LAB 4.3.1.7 asks you to write and test a function, days_in_month, that takes two arguments, a year and a month, and returns the number of days in that month. The function must be universal, so it can handle any month, though only February is sensitive to whether the year is a leap year. If the arguments do not make sense, such as an invalid month number, the function should return None instead of a numeric answer.
A useful technique the lab encourages is storing the lengths of all twelve months inside a list. A list such as [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] lets you index directly to the day count for any month, with the leading 0 making the list align so that month 1 maps to January, month 2 to February, and so on. This shortens the code considerably compared to checking each month with a long if chain.
Because February has 29 days in a leap year and 28 otherwise, the function must first determine whether the year is a leap year. The post relies on a companion function, is_year_leap, built in the earlier lab, which here returns True for years such as 2000 and 2016 and False for years like 1900 and 1987. Depending on that result, the function selects either a leap-year month list, with February set to 29, or a regular list with February set to 28, and returns the appropriate day count.
The lab is completed by a test harness that runs the function against known pairs of years and months and compares each result with the expected number of days. Printing "OK" or "Failed" for each case gives immediate feedback and confirms the function behaves correctly. Testing functions against a table of expected outputs is a core habit the PCAP course wants you to develop.
days_in_month(year, month) takes a year and a month and returns the number of days in that month.is_year_leap decides whether February should have 29 or 28 days.None.All months except February have fixed lengths. February normally has 28 days, but it gains an extra day to 29 during a leap year, making it the only month whose length depends on the year.
The leading 0 keeps the list indexes aligned with month numbers, so index 1 stores January's length, index 2 stores February's, and so on, letting you look up any month directly by its number.
If the arguments do not make sense, such as a month number outside the range 1 to 12, the function should return None to signal that no valid day count exists.
It loops over a set of known year and month pairs, calls days_in_month for each, and compares the returned value with an expected number of days, printing "OK" or "Failed" for every case.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.