1 00:00:00,140 --> 00:00:03,010 Before we look at how I solve this, I wanna say that my solution 2 00:00:03,010 --> 00:00:06,190 isn't necessarily the cleanest, fastest, or best. 3 00:00:06,190 --> 00:00:09,370 My solutions also aren't the only solutions. 4 00:00:09,370 --> 00:00:12,200 I tried to stick to solutions like the ones you might have come up with. 5 00:00:12,200 --> 00:00:13,540 But please, experiment and 6 00:00:13,540 --> 00:00:16,890 see if you can find better solutions than the ones that I'm showing. 7 00:00:16,890 --> 00:00:21,680 Okay, so, problem 1 needed us to loop through the people and 8 00:00:21,680 --> 00:00:23,290 see if they like to celebrate their birthday. 9 00:00:23,290 --> 00:00:26,670 And if they do, we print out Happy Birthday and their name. 10 00:00:26,670 --> 00:00:32,480 So, first, we'll do something like for person in BIRTHDAYS. 11 00:00:32,480 --> 00:00:37,640 Because that will give us each person as we move through the loop. 12 00:00:37,640 --> 00:00:39,530 And it'll put it into a variable called person, and 13 00:00:39,530 --> 00:00:43,130 we'll move through everything that's in BIRTHDAYS, which is what we want. 14 00:00:43,130 --> 00:00:50,910 So, we're calling this a person, not just a name, okay? 15 00:00:50,910 --> 00:00:53,010 So that gives us the variable name person, 16 00:00:53,010 --> 00:00:55,670 on the BIRTHDAYS as we go through the iterations of the loop. 17 00:00:55,670 --> 00:00:59,880 The first time through, it'll be James, then Shawna, then Amaya, and so on. 18 00:00:59,880 --> 00:01:06,060 Okay, so, the third item zero, one, two, three, the third item, 19 00:01:06,060 --> 00:01:10,480 so index 2 for number three in the person tuple 20 00:01:10,480 --> 00:01:14,320 is the true false boolean of whether or not they like to celebrate their birthday. 21 00:01:14,320 --> 00:01:18,380 Now if that's true, we want to print out their name and the Happy Birthday string. 22 00:01:18,380 --> 00:01:22,581 So, we can say if person[2] which is the, this is the zero item, 23 00:01:22,581 --> 00:01:27,240 this is the one item, this is the two item, this is the three item. 24 00:01:27,240 --> 00:01:31,920 So the two item, if that's true, so just if person[2], cuz if it's true, 25 00:01:31,920 --> 00:01:34,180 it will be true, if it's false, it will be false. 26 00:01:34,180 --> 00:01:38,810 Then we want to print Happy Birthday. 27 00:01:38,810 --> 00:01:44,900 I'll do a comma and then a placeholder, and I will format that with person [0], 28 00:01:44,900 --> 00:01:48,390 which will get their name, all right? 29 00:01:48,390 --> 00:01:51,627 So I'm gonna save that and then I'm gonna come down here and 30 00:01:51,627 --> 00:01:53,646 I'm gonna run it, python for.py. 31 00:01:53,646 --> 00:01:57,387 And I should see Happy Birthday, James, Happy Birthday, Shawna, 32 00:01:57,387 --> 00:01:58,814 Happy Birthday, Kamal. 33 00:01:58,814 --> 00:02:02,105 So James likes to celebrate, Shawna likes to celebrate, and 34 00:02:02,105 --> 00:02:03,830 Kamal likes to celebrate. 35 00:02:03,830 --> 00:02:07,690 So, great, that looks like we did that one. 36 00:02:07,690 --> 00:02:10,490 Problem 2 is a bit trickier. 37 00:02:10,490 --> 00:02:13,040 We have to look through all the people again, but this time, 38 00:02:13,040 --> 00:02:14,848 we need to calculate their half birthday. 39 00:02:14,848 --> 00:02:18,652 All right, so, let's start off, just like we did before, 40 00:02:18,652 --> 00:02:22,769 with for person in BIRTHDAYS, so that would give us each person. 41 00:02:22,769 --> 00:02:25,229 Now I'm gonna make a couple of variables here, so 42 00:02:25,229 --> 00:02:28,020 that I can separate out their name and their birthdate. 43 00:02:28,020 --> 00:02:34,290 So I'm gonna do name = person(0), cuz that's the first item in the person tuple. 44 00:02:34,290 --> 00:02:38,580 And then I'm gonna do birthdate = person[1]. 45 00:02:38,580 --> 00:02:43,490 And then, I know that these are all day slash month. 46 00:02:43,490 --> 00:02:46,340 So I want the day and I want the month as their own thing. 47 00:02:46,340 --> 00:02:49,970 So I'm gonna split that on the forward slash. 48 00:02:49,970 --> 00:02:53,726 So birthdate will now be a list, and the first item on the list will be the date. 49 00:02:53,726 --> 00:02:55,470 And the second item on the list will be the month. 50 00:02:56,810 --> 00:03:04,660 Cool, so now let's turn their birth month into a number so we can do math on it. 51 00:03:04,660 --> 00:03:09,391 So we'll say birthdate[1], so the second item 52 00:03:09,391 --> 00:03:14,354 in the birthdate, is the int(birthdate[1]). 53 00:03:14,354 --> 00:03:17,660 And then, this is the place where we might differ. 54 00:03:17,660 --> 00:03:19,700 So, you can look at the birthdate and month, and 55 00:03:19,700 --> 00:03:24,780 if it's bigger than six, then just subtract six from it. 56 00:03:24,780 --> 00:03:27,380 That will stop you from doing what I'm gonna do, which is, 57 00:03:27,380 --> 00:03:29,680 then I'm gonna go ahead I'm just gonna add 6 here. 58 00:03:29,680 --> 00:03:31,700 So I'm always going to add 6. 59 00:03:31,700 --> 00:03:35,384 And now if birthdate[1] is greater than 12, 60 00:03:35,384 --> 00:03:40,385 which means we looped around the year, and we don't wanna do that, 61 00:03:40,385 --> 00:03:44,436 then, birthdate[1] = birthdate[1]- 12. 62 00:03:44,436 --> 00:03:48,153 I can also do birthdate -= 12, 63 00:03:48,153 --> 00:03:52,260 and that will give me the same output. 64 00:03:52,260 --> 00:03:53,470 So, either way here, 65 00:03:53,470 --> 00:03:56,820 it's up to you, whichever one of those you would like to do. 66 00:03:58,540 --> 00:04:02,810 Okay, so now, I need to turn that back into a string 67 00:04:02,810 --> 00:04:06,300 because I wanna join the two pieces back together. 68 00:04:06,300 --> 00:04:10,824 So I'm gonna change birthdate[1] to be 69 00:04:10,824 --> 00:04:15,860 equal to the str(birthdate[1]), okay? 70 00:04:15,860 --> 00:04:21,145 And now, I'm going to print(name, 71 00:04:21,145 --> 00:04:25,950 "/".join(birthdate)). 72 00:04:25,950 --> 00:04:28,491 So that will print out their name. 73 00:04:28,491 --> 00:04:31,693 And then on the same line, it will print the day and the month, 74 00:04:31,693 --> 00:04:35,527 including the new month since we changed it, joined together by a slash. 75 00:04:35,527 --> 00:04:38,510 Okay, so now that should do it. 76 00:04:38,510 --> 00:04:40,870 So let's test that one. 77 00:04:40,870 --> 00:04:47,340 So python for.py, and see if we can get this one on screen. 78 00:04:48,580 --> 00:04:49,530 There we go. 79 00:04:49,530 --> 00:04:51,072 So we'll just check a couple of these. 80 00:04:51,072 --> 00:04:54,250 Let's see, let's check, Kamal was one we figured out before. 81 00:04:54,250 --> 00:04:58,373 So 29/4 should become 29/10, and it did. 82 00:04:58,373 --> 00:05:04,185 And let's see, 14/3 should become, 14/9, so it did. 83 00:05:04,185 --> 00:05:07,826 And 12/6 should become 12/12. 84 00:05:07,826 --> 00:05:09,760 Yeah, cool, I think we did that. 85 00:05:11,280 --> 00:05:14,880 All right, let's move on to problem number 3. 86 00:05:14,880 --> 00:05:19,232 So for problem number 3, we only wanna deal with school birthdays, 87 00:05:19,232 --> 00:05:23,630 only people who have birthdays during the school year. 88 00:05:23,630 --> 00:05:26,700 Now this one's kind of a combo of problems 1 and 2. 89 00:05:26,700 --> 00:05:28,690 You should know the first couple of steps here, so 90 00:05:28,690 --> 00:05:30,790 I'm not gonna really spell them out. 91 00:05:30,790 --> 00:05:33,969 So for person in BIRTHDAYS, 92 00:05:33,969 --> 00:05:39,271 you all know this part, name = person[0]. 93 00:05:39,271 --> 00:05:46,957 And birthdate = person[1].split on the slash. 94 00:05:46,957 --> 00:05:53,370 And birthdate[1] = int(birthdate[1]), right? 95 00:05:53,370 --> 00:05:56,060 That's familiar, that's what we just did. 96 00:05:56,060 --> 00:06:00,990 Now I'm going to use the range function here to get a couple of ranges of numbers. 97 00:06:00,990 --> 00:06:05,530 You could do this with greater than less than comparisons if you wanted. 98 00:06:05,530 --> 00:06:10,461 So I'm gonna say if birthdate[1] in range(1, 99 00:06:10,461 --> 00:06:14,355 7), because I wanna get the months one through six. 100 00:06:14,355 --> 00:06:17,135 And ranges stop when they get to the big number. 101 00:06:17,135 --> 00:06:19,455 So this one will only go 1 to 6 and won't include the 7. 102 00:06:20,515 --> 00:06:26,070 Or birthdate[1] in range(9, 13), same idea. 103 00:06:26,070 --> 00:06:27,595 Then we're going to print(name). 104 00:06:29,225 --> 00:06:31,325 That should be the solution to that one. 105 00:06:31,325 --> 00:06:32,915 So let's test that out. 106 00:06:34,830 --> 00:06:38,640 And again, a little bit tricky to scroll here. 107 00:06:40,470 --> 00:06:42,940 Let's just run it again, there we go. 108 00:06:42,940 --> 00:06:47,990 Okay, so, if their birthdays are between 1 and 6 and 9 and 12, 109 00:06:47,990 --> 00:06:52,750 so James is not, but Shawna is. 110 00:06:52,750 --> 00:06:57,512 Shawna's the beginning of June, so she has a birthday, and that comes out. 111 00:06:57,512 --> 00:07:00,640 Amaya is in February, so there's a birthday. 112 00:07:00,640 --> 00:07:03,360 Kamal has one and Xan has one. 113 00:07:03,360 --> 00:07:06,200 But Sam doesn't because Sam was born in July. 114 00:07:06,200 --> 00:07:07,820 James doesn't because James was born in August. 115 00:07:07,820 --> 00:07:12,380 So those two are the summer babies and they don't have school birthdays. 116 00:07:12,380 --> 00:07:16,410 So they always get that end of the year birthday party or whatever, right? 117 00:07:17,890 --> 00:07:21,350 Okay, so now let's try problem number 4. 118 00:07:21,350 --> 00:07:24,630 Now I think problem number 4 might seem hard, but 119 00:07:24,630 --> 00:07:27,230 I think it's actually really pretty simple. 120 00:07:27,230 --> 00:07:30,790 It's an amalgamation of a lot of the things that we've already done. 121 00:07:30,790 --> 00:07:33,354 So we need to get some data out of each person like before. 122 00:07:33,354 --> 00:07:35,953 And then we need to create a bunch of Stars that they're celebrating and 123 00:07:35,953 --> 00:07:36,970 if they're young enough. 124 00:07:36,970 --> 00:07:41,970 So, this one shouldn't introduce too many new things. 125 00:07:41,970 --> 00:07:44,174 So let's say for person in BIRTHDAYS. 126 00:07:46,202 --> 00:07:53,870 And again, we're gonna do the name = person[0], age = person[-1]. 127 00:07:53,870 --> 00:07:55,783 So that will get the last thing from person. 128 00:07:55,783 --> 00:08:00,690 I could have also done person[3], but -1 is just a little bit easier. 129 00:08:00,690 --> 00:08:06,560 And then celebrates is going to be person[-2]. 130 00:08:06,560 --> 00:08:08,510 So up here we did, what did we do? 131 00:08:08,510 --> 00:08:09,580 We did person[2]. 132 00:08:10,760 --> 00:08:12,645 They work out to be the same thing, 133 00:08:12,645 --> 00:08:16,351 whether you go from the front of the tuple or to the back of the tuple. 134 00:08:18,082 --> 00:08:20,221 And then we need to do an if condition. 135 00:08:20,221 --> 00:08:25,829 So, if they celebrate, so if celebrates and 136 00:08:25,829 --> 00:08:29,300 age is less than or = 10. 137 00:08:29,300 --> 00:08:32,330 And age, if we look at our tuple again is a number, so 138 00:08:32,330 --> 00:08:33,650 we don't have to convert it to anything. 139 00:08:35,190 --> 00:08:39,604 So if they're under 10, we could've also done if age is less than 11. 140 00:08:39,604 --> 00:08:41,143 But I think less than or 141 00:08:41,143 --> 00:08:45,450 equal to 10 is more obvious of the logic we're trying to use. 142 00:08:46,670 --> 00:08:50,480 Okay, so now, let's say stars is equal to an empty string. 143 00:08:51,570 --> 00:08:55,328 And then for star in range(age), so for however old they are, 144 00:08:55,328 --> 00:08:57,354 we're gonna make a range of that. 145 00:08:57,354 --> 00:09:01,920 And then, we're going to do stars +=, and I'm gonna use an asterisk. 146 00:09:01,920 --> 00:09:05,890 Now if you wanted to use some other fancier star, you can totally do that. 147 00:09:05,890 --> 00:09:08,662 So then back here where the for loop is, 148 00:09:08,662 --> 00:09:13,119 then at this point, we're gonna say print(name, stars). 149 00:09:13,119 --> 00:09:15,560 So I'm gonna print the name and the stars on the same line. 150 00:09:16,642 --> 00:09:19,765 Now, there's a much more clever way to build the string of stars too. 151 00:09:19,765 --> 00:09:21,590 I'm gonna let you find that though. 152 00:09:21,590 --> 00:09:24,592 And as a hint, look up list comprehension. 153 00:09:24,592 --> 00:09:26,040 Okay, so let's test this one. 154 00:09:26,040 --> 00:09:29,206 And again, let's just pull this up. 155 00:09:29,206 --> 00:09:34,634 And we'll do python for.py. 156 00:09:34,634 --> 00:09:41,067 And if we look up here at our list, so James is 9 and has a True. 157 00:09:41,067 --> 00:09:43,532 Amaya is 8 but has a False. 158 00:09:43,532 --> 00:09:45,730 And no one else is under the age of 10. 159 00:09:45,730 --> 00:09:48,010 So James is the only one we should print out, we should print out nine stars. 160 00:09:48,010 --> 00:09:52,840 So there's two, there's four, there's six, there's eight, there's nine, so 161 00:09:52,840 --> 00:09:55,320 there's James and nine stars. 162 00:09:55,320 --> 00:09:57,610 So great, we got all four problems. 163 00:09:57,610 --> 00:10:00,870 Hopefully, you've got a bit of a better grasp on how to use for loops now. 164 00:10:00,870 --> 00:10:03,890 In the next video, we'll check out the while loops problems.