Solution Design Any integer is a valid value for the year parameter, but we do need to pass year to our
leapyear.isLeapYear()
function if month is set to 2 (February). Be sure your program has an
import leapyear
instruction and that the leapyear.py file is in the same folder as your solution’s file.
Any month values outside of 1 to 12 would be an invalid date. In Python, you can chain
together operators as a shortcut: the expression 1 <= month <= 12 is the same as the expression
(1 <= month) and (month <= 12)
. You can use this to determine if your month and day
parameters are within a valid range.
The number of days in a month depends on the month:
If month is 9, 4, 6, or 11 (September, April, June, and November, respectively) the
maximum value for day is 30.
If month is 2 (February), the maximum value for day is 28 (or 29 if
leapyear.isLeapYear(year)
returns True).
Otherwise, the maximum value for day is 31.
Like the solution for Exercise #20, ―Leap Year,‖ this solution is a series of if, elif, or else
statements.
Special Cases and Gotchas To simplify your code as much as possible, think about the cases that can cause the function to
return as soon as possible. For example, if month is outside of the 1 to 12 range, the function returns
False
. There’s no other set of circumstances you need to check; the function can immediately return
False
. If it doesn’t return, you can assume that month contains a valid value for the rest of the
function.
The other immediate check is if leapyear.isLeapYear(year) returns True and month is
2
and day is 29, the function can immediately return True. If the function hasn’t returned True for
the leap day, you can assume that leap years are irrelevant for the rest of the code in the function.
Now try to write a solution based on the information in the previous sections. If you still have
trouble solving this exercise, read the Solution Template section for additional hints.