livepython.net
CHAPTER 11While Loops
11.5  PRACTICE

Python while Loop Practice Questions for Beginners

Chapter 11 Recap and Practice

Your programs can now repeat until something happens. Here is the chapter in one table:

You writeIt does
while count <= 5:repeats the indented block as long as the condition is True
start, check, changethe three parts of a counter loop: n = 1, while n <= 5:, n += 1
while height <= 100:loops until a goal is reached, however many rounds it takes
a loop with no changean infinite loop: the condition never turns False
while True: + breakloops until the break runs; make sure it's reached
< or <=the off-by-one choice: check the first and last value
while marks < 0 or marks > 100:asks again until the answer is valid
while item != "done":stops at a stop word (a sentinel value)
while pin != "1234" and tries < 3:limits the tries; an if after the loop says why it ended
while tasks:runs until the list is empty
for or whilefor when you know what to go through, while when you know when to stop

And the checklist for any while loop: does a line inside the loop change the condition's variable, in the right direction, without skipping the stop value, and before any continue?

Practice questions

Use input() with no prompt text. The tests type every line for you, and the last line always ends the loop: a stop word, a valid answer or a right guess. Test your program with those edges too.

The while loop

  1. 90.Python Program to Print Numbers from 1 to N Using a While LoopEasySolve
  2. 91.Python Program to Print a Countdown with a While LoopEasySolve
  3. 92.Python Program to Find the Sum of Digits of a NumberMediumSolve

Infinite loops

  1. 93.Fix the Endless Countdown LoopEasySolve
  2. 94.Python Program to Count the Weeks to Reach a Savings GoalMediumSolve

Input loops

  1. 95.Python Program to Add Numbers Until the User Types 0EasySolve
  2. 96.Python Program to Ask Again Until the Age Is ValidEasySolve
  3. 97.Python Program to Find the Largest Number Until done Is TypedMediumSolve

while or for

  1. 98.Python Program to Count the Steps of the Collatz SequenceMediumSolve
  2. 99.Python Program to Make a Number Guessing GameHardSolve

Now your programs can repeat until the job is done. Next, you'll learn everything you can do with strings.