The Problem: For Loops3:23 with Kenneth Love
For loops are used to process steps one at a time, and you have a bit of control over how you step through each pass of a loop.
[MUSIC] 0:00 Hey there. 0:04 I know that loops, both the for and 0:05 the while varieties, can be a bit tricky to understand. 0:07 We're used to doing things repetitively in real life, but 0:10 sometimes translating things from real life to programming can be a bit weird. 0:12 So let's do a bit more practice with loops so 0:16 they will hopefully make a bit more sense. 0:18 I have two files here in the workspace. 0:21 The first one is named for.py and that is for practicing for loops. 0:23 And the second one, while.py, is for practicing while loops. 0:27 So I'm gonna show you the problems for each of them in the appropriate videos. 0:32 You can then stop the video and write the code yourself to solve the problems. 0:36 If you get stuck or you just wanna see how I solved my problem, 0:40 then just watch the next video and you'll see how I did it, all right? 0:43 Let's get started with for loops. 0:46 For a brief refresher on for loops, 0:49 they're loops that work their way through the items in an iterable. 0:52 So you'd have for and then some_variable in [1, 2, 3], all right? 0:57 Something like that. 1:05 For each iteration of the loop, each trip through it, it will take the next item 1:06 from the iterable, and it will put it into whatever variable name we've given. 1:10 And then it will execute the code that's inside the loop and 1:15 the loop will end when the last item is processed. 1:19 Here in for.py, I have a few problems outlined in the comments. 1:25 And at the top of the file there is a tuple of people, their birthdays, whether 1:29 or not they like to celebrate their birthday, and how old they're going to be. 1:34 If you're not sure what a tuple is, 1:37 it's just like a list except you can't change its contents. 1:38 It's actually the reason why I'm using a tuple. 1:42 So that you don't accidentally change anything. 1:43 One less thing for you to worry about. 1:45 So let's look at these problems. 1:47 For the first one, called Celebrations, I want you to loop through each person 1:49 that's in the tuple, and if they celebrate their birthday, print out their name. 1:53 Print out like, Happy Birthday Sam. 1:58 Although Sam doesn't celebrate, so that wouldn't be the right one to print. 2:01 For the second problem right here, Problem 2: Half Birthdays, 2:05 you're going to have to do some math. 2:07 Loop through the tuple again and for each person figure out their half birthday. 2:10 If you're not familiar with a half birthday, 2:13 it's the day that's exactly six months after or before your birthday. 2:16 So it's your birthday on the other side of the year. 2:19 A little extra reason to eat cake never hurt, right? 2:22 For example, for Kamal here, their birthday would be October 29th, 2:25 since they were born in April 29, or 2:30 29/10, keeping this day slash month format. 2:33 The third problem is similar to the first. 2:38 Loop through the tuple again and for each person, if their birth month is between 2:40 9 and 6, so 9 to 12 or 1 to 6, then print out their name. 2:45 So if they have their birthday during a typical school year. 2:49 And finally, for 2:54 the fourth program, you can probably guess, the first step is a loop. 2:54 Then if the person celebrates their birthday and they're ten years old or 2:59 less, print out however many stars their age is. 3:02 For example, if they're turning nine, you'd print out nine stars. 3:05 Feel free to use any and all Python techniques that you're comfortable with 3:09 for this practice session. 3:11 The only thing you should definitely try and use for every problem is a for loop. 3:13 You might need to turns strings into numbers, create ranges, and more though. 3:17 I'll see you in the next video for my solution. 3:21
You need to sign up for Treehouse in order to download course files.Sign up