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