Course 4.2Lesson 1 of 5
A while loop keeps repeating its indented lines as long as its condition is True. Python checks the condition before each round, and the loop ends when the condition is False. Something inside the loop must change, so that the condition can become False, or the loop never stops. Use while when you do not know the number of rounds: saving until there is enough, doubling until a size is passed, or removing the digits of a number one at a time.
Try the code and the simulations beside the lesson.
After this lesson you can
while loop with a condition and a body,A for loop is for a number of rounds that you know: ten times, once for each letter. But many jobs are different. Save money until you have enough for a book. Wait until the water is hot. Ask a question until the answer is right. You do not know in advance how many rounds are needed. You know only the reason to continue.
For such jobs there is the while loop. It repeats as long as a condition is True.
Follow the program, like a computer.
count starts at 1.count <= 3? Yes. It runs the body: it shows Round 1, and then adds 1 to count.2 <= 3? Yes. Round 2. Now count is 3.3 <= 3? Yes. Round 3. Now count is 4.4 <= 3? No. The loop is over. The program goes on with the first line that is not indented.The condition is asked before every round, even the first. If it is False at the start, the body never runs.
The line count = count + 1 is the most important line of the loop. It makes the condition become False one day. If we forget it, count stays 1 for ever, the condition is always True, and the loop never stops. This is an endless loop. We look at it again in the last lesson.
A good rule: every while loop needs a line in its body that moves the condition towards False.
Here is the job that a for loop cannot do easily. You have 0 rupees and save 70 each week. After how many weeks can you buy a book that costs 500?
The total can go a little past the goal, because the loop adds a whole week's savings each time.
Another job with an unknown number of rounds: a plant doubles its height every day. After how many days is it taller than 100 centimetres?
A beautiful use of while: take a number apart, digit by digit. The last digit of a number is number % 10. Removing the last digit is number // 10.
count = count - 1 makes count <= 3 stay True for ever.< instead of <=. The loop is one round short (the last lesson looks at this).while count = 3: is a SyntaxError.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
while condition: repeats its indented body as long as the condition is True.False.while when you do not know the number of rounds.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
False at the start?count = count + 1 in the first program?4372 % 10? And 4372 // 10?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.