Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
The Solution: While Loops6:52 with Kenneth Love
Here's how I did it!
So, did those problems take a while? 0:00 I feel like the while-loop is a bit of a simpler concept than the for-loop, 0:02 probably because it's just a condition with no variable or iteration needed. 0:06 Let's look at how I solve these problems. 0:09 The first problem, heating the oven is a pretty normal use case for 0:12 a while-loop, so we'll say while current_oven_temp < 350. 0:17 So as long as it's under 350, we can go ahead and increase it, right, 0:23 so current_oven_temp + = 25, and then print current_oven_temp. 0:28 Now, you could do these in either order depending on how you read 0:33 the instructions. 0:35 Maybe you feel like you should print it before you increase it or 0:36 increase it before you print it. 0:41 It really doesn't matter though and if it's not less than 350, 0:43 so this would be where else comes in, then we want to print out, the oven is ready. 0:47 Now, we could do this without using the else, but 0:55 I like to include them as it makes it a bit more obvious what's going on. 0:57 So let's go ahead and test this one, so we'll do python while.py. 1:01 And we can see we've got 100, 125, 150 and so on, until we get to 350 and 1:07 then the oven is ready. 1:11 Great, so it looks like we're getting all those intermediate degrees. 1:14 Onto problem number two. 1:16 Okay, so the first thing that I have to do is have an infinite loop. 1:18 So I'm gonna go ahead and do while true, which is a loop that will never end. 1:23 You could also do say while one, or while a, or 1:28 anything that's always going to evaluate as true. 1:31 So then I'm gonna ask for a number. 1:36 Now, I'm gonna go ahead and I'm gonna lowercase it, which, of course, won't have 1:37 any effect on the numbers, but it makes the queue comparison a little easier. 1:41 So I'm gonna say, num = input. 1:44 And i'm gonna say, give me a number or q to quit, 1:47 and then I'm going to call lower on that. 1:52 All right, so now let's do that comparison. 1:57 So if num == 'q' then break. 1:59 Now, I'm gonna use break here to immediately end the while-loop. 2:04 Now, like I said, 2:08 you could do this without the break, but I like to have the break in there. 2:08 Okay, so now, let's try to turn it into a number, 2:13 to be a bit more flexible, I'm just gonna turn them all into floats. 2:16 But you could make whatever decision you want here, if you wanna make them ints, 2:19 floats, whatever, as appropriate. 2:22 So I'm gonna do, try: numbers.append(float(num)), 2:24 the float version of the num. 2:29 And then the exception that I'm going to catch is, ValueError. 2:32 And if the ValueError comes up, then I'm gonna do, continue, so 2:36 I'm just gonna start that loop over again, okay? 2:38 So if anybody gives me something that's not a number and it's not q, 2:41 then we're just gonna ignore it. 2:44 We're gonna pretend it didn't even happen and we're just gonna go on to the next 2:46 step of the loop, which comes right back to the input, all right. 2:50 Now, I could leave out the continue, I could put in, say, pass instead. 2:53 They're effectively the same in this case, but 2:57 continue is like the more obvious thing. 2:59 Okay, so now, we're done with the loop's code. 3:02 Because at this point they have either, they've even wanna put in a new number, or 3:06 they've give us a q and they wanna quit. 3:10 So, I can't use the else here that I would normally have used, because this 3:13 break counts as the loop ending early and so, that makes it skip the else clause. 3:20 So I'm just going to do the code that I wanted to do here. 3:24 So I'm gonna print out you entered, That, and then the numbers that they entered, 3:28 and then I will print out the total is, and then the sum of the numbers. 3:35 And sum, if you haven't heard of this function before. 3:42 We'll take a list of numbers, and it will add them all together, 3:45 and then I will print, the average is, and 3:51 then we will print sum(numbers)/len(numbers). 3:55 And all right, so now that should do it. 4:00 So this is gonna call total_and_average for us, so 4:03 let's go ahead and python while.py. 4:08 Give a number, okay. 4:11 So, let's do 5, 5, 5, and 5, and then let's do a queue. 4:12 So we entered 5, 5, 5, and 5. 4:20 The total is 20 and the average of that is 5, cool. 4:23 So that's handy I guess, depending on your needs for calculating averages and sums. 4:27 Okay, so now, let's try problem number 3, this is our last problem. 4:36 Now, this one needs us to skip over numbers that aren't evenly divisible by 4:42 three or five. 4:45 Now, initially, I was thinking that I'd want to use continue on this, but 4:47 there's really no reason to. 4:49 Much like problem number one, we need a condition in our loop. 4:51 So we're gonna say while current > 101. 4:54 Then we'll check the division, or divisibility of the number. 4:59 Now to do this, 5:03 I'm going use the modulo operator, which returns the remainder from division. 5:04 So, if not, current % 3, or 5:09 current % 5 == 0, then print(current). 5:13 So you probably don't wanna do this exactly like me. 5:19 I'm showing you deliberately two different ways of testing this right now. 5:22 You pick which ever one's more obvious to you. 5:26 So first we have the not current % 3, and 5:28 this one will evaluate the true if the remainder is 0. 5:32 Because 0 is falsy and the not swaps it. 5:36 So instead of being falsy, it becomes truthy because of the not, 5:40 and then the current % 5 is equal to 0. 5:45 It should be pretty obvious, but we're comparing to see if when I do modulo of 5:48 modulo 5 against current if that comes out to 0. 5:53 So if so, that becomes true. 5:56 So if this is true or 5:58 if this is true, if either one of them is true then print out the current number. 6:00 Now, finally, we need to increment the number. 6:05 So move up to the next number, and that should do it, let's test that out. 6:13 So python while.py, now, I'm gonna get asked for a number. 6:20 I'm gonna put in a 1 and q, and then we get our numbers in here. 6:24 So like 99 is divisible by three, 96 is divisible by three, 100s divisible by 5, 6:29 90s divisible by three. 6:34 But we're not getting like say 82 which isn't divisible by 5 or 3. 6:36 So awesome that's all of our numbers. 6:41 Thanks for practicing loops with me. 6:43 Hopefully, this will help you get 6:45 more comfortable with these super useful constructs. 6:45 Be sure to let us know if any other topics that you'd like to see a bit 6:48 more practice on. 6:51
You need to sign up for Treehouse in order to download course files.Sign up