In this exercise, you are going to be building
a program that can tell us how many days
there are in any given month for any given year.
In the Input pane,
you'll see the first line we can input any given year
and on the second line we can input any month.
And what your program is going to do,
is it's going to take those years and month
and figure out how many days there are in that month.
So why do we care about the year?
Well, in leap years, on February,
there's actually 29 days instead of the usual 28.
So this is a great chance for us to practice what we learned
about functions with outputs
and also see some code that we're already familiar with,
which is the code that we use
to work out whether if a year is a leap year.
If you look at the starting code,
you'll see that there is a function called is_leap,
which takes an input of "year"
and we've got the code which comes
from the solution code of the leap year exercise.
Now, if you did it differently,
feel free to modify this function,
it is just there to help you get started.
What you want to do,
is instead of printing leap year or not leap year
you want to make this function have a return.
So it should output either "True",
if it is a leap year, or "False" if it's not a leap year.
And then inside the function days_in_month,
you're going to use this is_leap,
to firstly check the inputs
and the inputs are going to be a year
and a month and you're going to create an output
for this function that gives us just in plain text,
the number of days in the month.
Take a look on line 21, where you've got the days_in_month
and then it takes two inputs,
firstly the year, secondly the month.
And you are going to have to make sure that that order
is respected when you modify the function days_in_month.
And essentially, we've got all of the days in a month
organized in a list called month_days
and you are going to work out, is it a leap year?
If not, then we're just going to use the usual standard days
for February and the rest of the year,
but if it is a leap year
and they want to check that it's February,
then we're going give them 29 instead of the usual 28.
That's the theory of it.
Now it's time to struggle and figure it out all on your own.
So give that a go.
The modification to our is_leap function is pretty easy,
if it originally said print is leap year
then we just return "True", otherwise we return "False".
And this is useful because then we can use this function
inside our other function, days_in_month.
And you know that we can call a function
from within another function.
It's setting us up
to help us when we write our days_in_month function.
Now the first part that we need to modify
for this days_in_month function, is the input parameters.
Notice how on line 22, where we've got the days and month
calling that function, we provide first the year,
then the month.
So we should respect that when we are writing our function,
so that it actually matches the inputs.
The way that we define a function with inputs is "def",
the name of the function, and then
inside the parentheses, the names of the input parameters.
Next, we're going to check if the month that is coming in
through the input for this function is equal to 2
and if we pass the year that's being put into the function,
happens to go through that is_leap function
and come out as True.
So if both of these conditions are True,
then it means the user is checking for a leap year
and they want to know the number of days in February,
in which case we return 29 days.
You have to imagine that on line 15 after the and,
what happens is the input comes in on line 13,
the year, and then we take that year
and we pass it into the is_leap function on line 15,
it goes through all of the lines
in the is_leap function and then at some point
it will return a value that could be True or False,
and the True or False is going
to replace the is_leap year part in line 15.
And then we end up having
an if statement that checks,
is both sides True between the "and" , so is the first part
of the condition True and after the "and",
is it also true in which case we will return 29.
Now the next step is,
if the user's not checking for a leap year February days,
then we can simply use that list where we've got the number
of days in a month, month_days and we pass in the index.
So notice how month_days is organized
from January all the way up to December,
so 1 to 12,
but of course, because we're working with a list in Python,
it starts with index 0.
So 31 is at position 0.
The month that's coming in
as an input is going to start from 1 up until 12.
So we need to subtract the input month by 1
in order to get it on the same frequency as the index
for the Python list.
And then we can simply use that as the index
inside the square brackets to access the number of days
in that month and return it as the output.
There are many ways that this program can fail
and one of those ways is if a user enters
a month that is beyond 12,
if they put in month 43, which obviously doesn't exist
and we've only got 12 values in our month days list.
So you could add in checks and balances
for these things as well, as well as many other edge cases.
But to keep it simple,
we've just got the main task addressed
in our program and hopefully
you managed to get pretty much all the way there.
If not, feel free to go back to your code,
edit it, and make sure that it works the way you want it to.