Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Practice Writing Loops in Python!
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
While loops are used to repeatedly loop through steps "while" a specific condition is true. Let's try this out with some examples.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
All right, now let's look at while.py.
0:00
There are only three
problems in this one but
0:02
they might prove more difficult
than the ones in for.py.
0:05
While loops,
0:07
if you don't remember, will execute until
their condition evaluates as false.
0:08
Because of this,
they're known as indefinite loops,
0:13
because they can run
an indefinite number of times.
0:15
For loops on the other hand,
will only run a certain number of times.
0:17
I don't think it's even possible to
make infinite for loops in Python.
0:21
You can use the continue keyword
to skip an iteration of the loop.
0:24
Or you can use the break keyword
to completely stop the loop.
0:28
Both of those keywords work on for loops
too, but they're used there less often.
0:31
Let's take a look at the problems.
0:36
The first problem, warm the oven,
needs you to add 25 degrees to
0:37
the current oven temperature
until it's at 350 degrees.
0:41
On each iteration,
print out the current temperature.
0:44
When the loop is done,
print out The oven is ready!
0:47
You can use the while loop's
else class here if you want.
0:50
For the second problem, you'll need
to finish the function that I started.
0:53
Yeah, I'm gonna call that started.
0:58
Anyway, create an infinite while loop.
1:00
Since while loops run until
their condition is false,
1:03
what value could you use to
make the condition never false?
1:06
While the loop is running,
ask the user for a number and
1:10
add that number to the numbers list.
1:12
If they give you a q, a string q,
instead though, end the loop.
1:14
When the loop ends, print out all of
the numbers, the sum of the numbers, and
1:19
the average of all of the numbers.
1:22
This solution doesn't absolutely
require you to use break, but
1:24
you'll probably find it
easier to solve that way.
1:28
Finally, the third problem.
1:31
You might have come across the common
programming problem known as fizzbuzz,
1:33
where you have to loop through all
of the numbers from say, one to 100.
1:37
And if the number is divisible by 3 or
5 or 7 or some combination of them or
1:40
some other number, then you print
out fizz, buzz, or fizzbuzz.
1:45
Now I don't want you to do exactly that,
but it's something similar.
1:49
Write a while loop that checks
the value of the current variable.
1:52
This one right here called current.
1:56
If it's 101, you should end the loop.
1:58
On each iteration,
increment the number by 1.
2:01
If the new number is divisible by 3, 5,
2:04
or both, print it out,
otherwise, skip the number.
2:07
All right,
see how you do on these challenges.
2:11
And I'll be back with
the solutions in a minute.
2:13
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up