1 00:00:00,360 --> 00:00:04,440 Head over to the Repl.it day 10.1 Days in Month 2 00:00:04,500 --> 00:00:06,810 coding exercise. Here, 3 00:00:06,840 --> 00:00:11,840 you'll notice that I've copied over the is_leap year function that we created 4 00:00:12,780 --> 00:00:16,260 a while back in another coding exercise. Now, 5 00:00:16,290 --> 00:00:21,240 the first thing I want you to do is to modify this function so that instead of 6 00:00:21,270 --> 00:00:24,150 printing leap year or not leap year, 7 00:00:24,510 --> 00:00:29,070 we're actually just going to return true if it is a leap year and false 8 00:00:29,130 --> 00:00:30,540 if it is not a leap year, 9 00:00:31,080 --> 00:00:34,890 because we're going to be using that function in the next function that you're 10 00:00:34,890 --> 00:00:35,723 going to modify, 11 00:00:36,210 --> 00:00:40,590 which is a function that's going to tell you how many days there are in the 12 00:00:40,590 --> 00:00:44,670 month that you specify. So for example, 13 00:00:45,090 --> 00:00:49,860 if I was to call days_in_month and passing in the year as 14 00:00:49,860 --> 00:00:52,800 2022 in the month as 2, 15 00:00:53,130 --> 00:00:55,950 then I should get an output of 28, 16 00:00:56,160 --> 00:01:00,240 as in there's 28 days in February, 2022. 17 00:01:00,870 --> 00:01:04,650 This is the reason why we need to know about leap years 18 00:01:04,680 --> 00:01:09,270 'cause in a leap year, there's actually 29 days in February. 19 00:01:09,840 --> 00:01:12,690 You're going to combine the output from this function 20 00:01:13,050 --> 00:01:16,530 as well as this list of days_in_month 21 00:01:16,830 --> 00:01:20,880 in order to modify this function to output the number of days in a month 22 00:01:21,030 --> 00:01:22,950 when this line of code is called. 23 00:01:23,430 --> 00:01:26,460 It's really important that you don't change any of the code below, 24 00:01:26,820 --> 00:01:31,140 but you are going to modify this function and add more lines of code in it. 25 00:01:32,280 --> 00:01:34,260 Take a look at the hints if you get stuck, 26 00:01:34,410 --> 00:01:37,020 but otherwise, pause the video and give it a go. 27 00:01:40,460 --> 00:01:40,790 All right. 28 00:01:40,790 --> 00:01:44,390 So the first thing I'm going to do is I'm going to change each of these print 29 00:01:44,390 --> 00:01:48,200 statements so that it returns a Boolean instead. 30 00:01:48,680 --> 00:01:52,880 So this one should return true, and this one should return true. Now, 31 00:01:52,880 --> 00:01:56,210 when it's not a leap year, I'm going to return false 32 00:01:56,270 --> 00:01:58,310 instead. This way, 33 00:01:58,310 --> 00:02:03,110 basically, when we call this function, is_leap, passing in a year, 34 00:02:03,440 --> 00:02:06,980 then we'll get the output as either true or false. 35 00:02:07,460 --> 00:02:11,540 So we can now use that inside our function days_in_month. 36 00:02:11,930 --> 00:02:14,570 But before we do that, we need to get some inputs. 37 00:02:14,810 --> 00:02:19,430 We need to know what is the year and what is the month the user is interested 38 00:02:19,430 --> 00:02:24,260 in. So that is going to be passed in using these positional arguments. 39 00:02:24,950 --> 00:02:28,640 We're going to ask the user to input a year and input a month, 40 00:02:28,970 --> 00:02:33,970 and then each of these pieces of data gets passed into here and here, 41 00:02:34,340 --> 00:02:37,340 which will end up inside our function ready for use. 42 00:02:38,600 --> 00:02:43,130 The first thing I'm going to check is if the year is a leap year. 43 00:02:43,940 --> 00:02:45,530 How are we going to do that? Well, 44 00:02:45,530 --> 00:02:50,000 we're going to use our function here, is_leap, passing in that year, 45 00:02:50,270 --> 00:02:53,960 and then the output will be checked using the if statement. 46 00:02:54,560 --> 00:02:59,560 Remember that the way that if statements work is that if something is evaluated to true, 47 00:03:00,490 --> 00:03:03,220 then it's gonna carry out the line of code below. 48 00:03:03,550 --> 00:03:07,510 But if it was evaluated and it turned out to be false, 49 00:03:07,720 --> 00:03:09,670 then it's going to skip this line of code. 50 00:03:10,390 --> 00:03:14,920 So instead of using true or false, or if a three is bigger than two, 51 00:03:15,250 --> 00:03:20,250 we're going to use the function, is_leap, and pass in the year that we got as an 52 00:03:21,310 --> 00:03:23,350 input. So this way, 53 00:03:23,380 --> 00:03:27,190 when a year is tested in this function, 54 00:03:27,580 --> 00:03:30,700 then it's going to output either true or false 55 00:03:30,730 --> 00:03:35,730 which is going to replace this part of the code because it's the output from 56 00:03:36,010 --> 00:03:39,670 this function. So now if that output is true, 57 00:03:40,060 --> 00:03:41,680 namely it is a leap year, 58 00:03:42,190 --> 00:03:46,780 then we're going to check if the month they were interested in was the month of 59 00:03:46,780 --> 00:03:50,290 February. So if the is_leap year 60 00:03:50,290 --> 00:03:55,000 is true and the month is equal to 2, well, 61 00:03:55,000 --> 00:03:55,810 in that case, 62 00:03:55,810 --> 00:04:00,810 then we're going to return 29 because there's 29 days in February in a leap year. 63 00:04:02,560 --> 00:04:04,240 If this is the case, 64 00:04:04,270 --> 00:04:08,920 then this function is going to end right here when it returns 29. 65 00:04:09,520 --> 00:04:11,440 But if this is not the case, 66 00:04:11,470 --> 00:04:16,329 then it's going to bypass this line of code and we can output the number of days 67 00:04:16,329 --> 00:04:20,110 in a month for any other condition, so when it's not a leap year. 68 00:04:20,829 --> 00:04:25,830 We can go ahead and simply just use the return statement to tap into that list 69 00:04:26,020 --> 00:04:27,700 called month_days 70 00:04:28,240 --> 00:04:32,380 and we're gonna pull out the value at the current month 71 00:04:32,410 --> 00:04:33,580 that's being requested. 72 00:04:34,030 --> 00:04:39,030 But remember that because lists start counting from zero and the month that the 73 00:04:39,250 --> 00:04:42,100 user's going to be interested in doesn't start from zero, 74 00:04:42,220 --> 00:04:45,460 so if they're interested in January then this month, 75 00:04:45,610 --> 00:04:49,660 instead of being 1, should actually be 0 to pull out this 31. 76 00:04:49,960 --> 00:04:52,990 So then we actually have to subtract one from it. 77 00:04:54,220 --> 00:04:58,120 So now if we test the code and we enter the year 78 00:04:58,120 --> 00:05:01,270 2022 and the month of February, 79 00:05:01,630 --> 00:05:04,900 then we'll get the correct output, which is 28. 80 00:05:05,890 --> 00:05:09,100 That solves the challenge. Now, some of you, I know, 81 00:05:09,100 --> 00:05:14,100 might have gone even further because we talked about using the return statement to 82 00:05:15,550 --> 00:05:20,320 catch out any situations where there might be invalid inputs. 83 00:05:20,680 --> 00:05:21,670 So you could, 84 00:05:21,700 --> 00:05:25,900 if you wanted to, also add an if statement that checks 85 00:05:25,900 --> 00:05:30,900 whether if the month is greater than 12 or if the month is less than one. In 86 00:05:34,390 --> 00:05:35,350 these cases, 87 00:05:35,380 --> 00:05:40,380 then it's actually an invalid input and you could just return invalid month. And 88 00:05:42,430 --> 00:05:44,710 you could of course do the same thing for the year. 89 00:05:44,980 --> 00:05:49,980 And this way it ensures that our function doesn't progress unless we have clean 90 00:05:50,800 --> 00:05:51,633 data. 91 00:05:52,270 --> 00:05:57,270 But this, of course, is not necessary to complete the challenge because we're now able 92 00:05:57,770 --> 00:06:02,330 to figure out the days in the month using this leap year function's output, 93 00:06:02,810 --> 00:06:07,810 and the output from our days_in_month function. In the next lesson, 94 00:06:08,900 --> 00:06:12,920 I'm going to address something that a lot of students are thinking at this 95 00:06:12,920 --> 00:06:16,460 point. And the question is why return at all? 96 00:06:16,850 --> 00:06:20,420 Why not just simply print the outputs instead, 97 00:06:21,320 --> 00:06:25,670 because this way it seems to end up doing the same thing, right? 98 00:06:25,730 --> 00:06:28,370 It's still going to print this down here. 99 00:06:28,850 --> 00:06:33,850 And if we just added a print to this return and a print here and got rid of this 100 00:06:35,030 --> 00:06:38,360 one, then it would still behave exactly the same. 101 00:06:39,140 --> 00:06:42,290 This is exactly what I want to talk about in the next lessons. 102 00:06:42,500 --> 00:06:43,430 So, see you there.