1
00:00:00,360 --> 00:00:02,730
In this exercise, you are going to be building

2
00:00:02,730 --> 00:00:05,850
a program that can tell us how many days

3
00:00:05,850 --> 00:00:09,030
there are in any given month for any given year.

4
00:00:09,030 --> 00:00:10,170
In the Input pane,

5
00:00:10,170 --> 00:00:13,023
you'll see the first line we can input any given year

6
00:00:13,023 --> 00:00:16,620
and on the second line we can input any month.

7
00:00:16,620 --> 00:00:18,570
And what your program is going to do,

8
00:00:18,570 --> 00:00:21,330
is it's going to take those years and month

9
00:00:21,330 --> 00:00:24,690
and figure out how many days there are in that month.

10
00:00:24,690 --> 00:00:26,370
So why do we care about the year?

11
00:00:26,370 --> 00:00:29,100
Well, in leap years, on February,

12
00:00:29,100 --> 00:00:32,907
there's actually 29 days instead of the usual 28.

13
00:00:32,907 --> 00:00:36,690
So this is a great chance for us to practice what we learned

14
00:00:36,690 --> 00:00:38,880
about functions with outputs

15
00:00:38,880 --> 00:00:41,910
and also see some code that we're already familiar with,

16
00:00:41,910 --> 00:00:43,290
which is the code that we use

17
00:00:43,290 --> 00:00:46,530
to work out whether if a year is a leap year.

18
00:00:46,530 --> 00:00:47,760
If you look at the starting code,

19
00:00:47,760 --> 00:00:50,130
you'll see that there is a function called is_leap,

20
00:00:50,130 --> 00:00:52,740
which takes an input of "year"

21
00:00:52,740 --> 00:00:54,300
and we've got the code which comes

22
00:00:54,300 --> 00:00:57,720
from the solution code of the leap year exercise.

23
00:00:57,720 --> 00:00:59,010
Now, if you did it differently,

24
00:00:59,010 --> 00:01:01,230
feel free to modify this function,

25
00:01:01,230 --> 00:01:04,560
it is just there to help you get started.

26
00:01:04,560 --> 00:01:05,393
What you want to do,

27
00:01:05,393 --> 00:01:07,920
is instead of printing leap year or not leap year

28
00:01:07,920 --> 00:01:10,830
you want to make this function have a return.

29
00:01:10,830 --> 00:01:13,170
So it should output either "True",

30
00:01:13,170 --> 00:01:17,040
if it is a leap year, or "False" if it's not a leap year.

31
00:01:17,040 --> 00:01:19,410
And then inside the function days_in_month,

32
00:01:19,410 --> 00:01:22,830
you're going to use this is_leap,

33
00:01:22,830 --> 00:01:25,950
to firstly check the inputs

34
00:01:25,950 --> 00:01:27,900
and the inputs are going to be a year

35
00:01:27,900 --> 00:01:31,230
and a month and you're going to create an output

36
00:01:31,230 --> 00:01:34,590
for this function that gives us just in plain text,

37
00:01:34,590 --> 00:01:36,810
the number of days in the month.

38
00:01:36,810 --> 00:01:41,250
Take a look on line 21, where you've got the days_in_month

39
00:01:41,250 --> 00:01:43,710
and then it takes two inputs,

40
00:01:43,710 --> 00:01:46,590
firstly the year, secondly the month.

41
00:01:46,590 --> 00:01:49,140
And you are going to have to make sure that that order

42
00:01:49,140 --> 00:01:53,130
is respected when you modify the function days_in_month.

43
00:01:53,130 --> 00:01:57,030
And essentially, we've got all of the days in a month

44
00:01:57,030 --> 00:02:00,360
organized in a list called month_days

45
00:02:00,360 --> 00:02:03,420
and you are going to work out, is it a leap year?

46
00:02:03,420 --> 00:02:06,810
If not, then we're just going to  use the usual standard days

47
00:02:06,810 --> 00:02:09,600
for February and the rest of the year,

48
00:02:09,600 --> 00:02:10,979
but if it is a leap year

49
00:02:10,979 --> 00:02:13,170
and they want to check that it's February,

50
00:02:13,170 --> 00:02:16,110
then we're going give them 29 instead of the usual 28.

51
00:02:16,110 --> 00:02:17,263
That's the theory of it.

52
00:02:17,263 --> 00:02:20,910
Now it's time to struggle and figure it out all on your own.

53
00:02:20,910 --> 00:02:21,903
So give that a go.

54
00:02:28,512 --> 00:02:33,480
The modification to our is_leap function is pretty easy,

55
00:02:33,480 --> 00:02:36,030
if it originally said print is leap year

56
00:02:36,030 --> 00:02:39,690
then we just return "True", otherwise we return "False".

57
00:02:39,690 --> 00:02:42,780
And this is useful because then we can use this function

58
00:02:42,780 --> 00:02:46,410
inside our other function, days_in_month.

59
00:02:46,410 --> 00:02:48,210
And you know that we can call a function

60
00:02:48,210 --> 00:02:50,100
from within another function.

61
00:02:50,100 --> 00:02:51,150
It's setting us up

62
00:02:51,150 --> 00:02:54,540
to help us when we write our days_in_month function.

63
00:02:54,540 --> 00:02:56,310
Now the first part that we need to modify

64
00:02:56,310 --> 00:03:01,310
for this days_in_month function, is the input parameters.

65
00:03:01,590 --> 00:03:05,400
Notice how on line 22, where we've got the days and month

66
00:03:05,400 --> 00:03:08,790
calling that function, we provide first the year,

67
00:03:08,790 --> 00:03:09,623
then the month.

68
00:03:09,623 --> 00:03:13,110
So we should respect that when we are writing our function,

69
00:03:13,110 --> 00:03:16,800
so that it actually matches the inputs.

70
00:03:16,800 --> 00:03:20,070
The way that we define a function with inputs is "def",

71
00:03:20,070 --> 00:03:21,690
the name of the function, and then

72
00:03:21,690 --> 00:03:25,833
inside the parentheses, the names of the input parameters.

73
00:03:27,330 --> 00:03:32,250
Next, we're going to check if the month that is coming in

74
00:03:32,250 --> 00:03:36,000
through the input for this function is equal to 2

75
00:03:36,000 --> 00:03:40,710
and if we pass the year that's being put into the function,

76
00:03:40,710 --> 00:03:43,350
happens to go through that is_leap function

77
00:03:43,350 --> 00:03:45,600
and come out as True.

78
00:03:45,600 --> 00:03:47,910
So if both of these conditions are True,

79
00:03:47,910 --> 00:03:51,660
then it means the user is checking for a leap year

80
00:03:51,660 --> 00:03:54,600
and they want to know the number of days in February,

81
00:03:54,600 --> 00:03:57,840
in which case we return 29 days.

82
00:03:57,840 --> 00:04:02,460
You have to imagine that on line 15 after the and,

83
00:04:02,460 --> 00:04:06,600
what happens is the input comes in on line 13,

84
00:04:06,600 --> 00:04:09,210
the year, and then we take that year

85
00:04:09,210 --> 00:04:13,980
and we pass it into the is_leap function on line 15,

86
00:04:13,980 --> 00:04:15,990
it goes through all of the lines

87
00:04:15,990 --> 00:04:19,290
in the is_leap function and then at some point

88
00:04:19,290 --> 00:04:23,100
it will return a value that could be True or False,

89
00:04:23,100 --> 00:04:25,110
and the True or False is going

90
00:04:25,110 --> 00:04:30,110
to replace the is_leap year part in line 15.

91
00:04:30,330 --> 00:04:31,650
And then we end up having

92
00:04:31,650 --> 00:04:33,450
an if statement that checks,

93
00:04:33,450 --> 00:04:37,770
is both sides True between the "and" , so is the first part

94
00:04:37,770 --> 00:04:39,870
of the condition True and after the "and",

95
00:04:39,870 --> 00:04:43,833
is it also true in which case we will return 29.

96
00:04:45,450 --> 00:04:46,980
Now the next step is,

97
00:04:46,980 --> 00:04:50,610
if the user's not checking for a leap year February days,

98
00:04:50,610 --> 00:04:54,120
then we can simply use that list where we've got the number

99
00:04:54,120 --> 00:04:58,710
of days in a month, month_days and we pass in the index.

100
00:04:58,710 --> 00:05:01,440
So notice how month_days is organized

101
00:05:01,440 --> 00:05:04,260
from January all the way up to December,

102
00:05:04,260 --> 00:05:06,630
so 1 to 12,

103
00:05:06,630 --> 00:05:09,178
but of course, because we're working with a list in Python,

104
00:05:09,178 --> 00:05:11,610
it starts with index 0.

105
00:05:11,610 --> 00:05:15,180
So 31 is at position 0.

106
00:05:15,180 --> 00:05:16,500
The month that's coming in

107
00:05:16,500 --> 00:05:20,010
as an input is going to start from 1 up until 12.

108
00:05:20,010 --> 00:05:23,130
So we need to subtract the input month by 1

109
00:05:23,130 --> 00:05:26,970
in order to get it on the same frequency as the index

110
00:05:26,970 --> 00:05:28,530
for the Python list.

111
00:05:28,530 --> 00:05:30,750
And then we can simply use that as the index

112
00:05:30,750 --> 00:05:34,050
inside the square brackets to access the number of days

113
00:05:34,050 --> 00:05:37,413
in that month and return it as the output.

114
00:05:38,610 --> 00:05:41,670
There are many ways that this program can fail

115
00:05:41,670 --> 00:05:44,580
and one of those ways is if a user enters

116
00:05:44,580 --> 00:05:47,250
a month that is beyond 12,

117
00:05:47,250 --> 00:05:50,790
if they put in month 43, which obviously doesn't exist

118
00:05:50,790 --> 00:05:54,510
and we've only got 12 values in our month days list.

119
00:05:54,510 --> 00:05:56,940
So you could add in checks and balances

120
00:05:56,940 --> 00:06:00,510
for these things as well, as well as many other edge cases.

121
00:06:00,510 --> 00:06:01,492
But to keep it simple,

122
00:06:01,492 --> 00:06:04,140
we've just got the main task addressed

123
00:06:04,140 --> 00:06:06,510
in our program and hopefully

124
00:06:06,510 --> 00:06:09,180
you managed to get pretty much all the way there.

125
00:06:09,180 --> 00:06:11,640
If not, feel free to go back to your code,

126
00:06:11,640 --> 00:06:14,733
edit it, and make sure that it works the way you want it to.

