A for loop is for a number of rounds that you know. A while loop is for the cases where you do not know: keep saving until there is enough, keep asking until the answer is right, keep going until something happens. In this course you write while loops, learn what makes them stop, leave a loop early with break, skip a round with continue, and ask the user again and again. A lesson on the loops that go wrong shows how to choose between for and while, and how to find an endless loop and a loop that is off by one. A guessing game with hints and a limit of tries is the project. The practice runs your loops and tells you which results are wrong.
5 lessons, about 2 h 30 min5 simulations14 problems10 practice questionsPython in every lesson
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.
break leaves a loop at once, and the program continues with the line after the loop. continue skips the rest of the current round and starts the next one. Both are used with an if inside the loop. while True with a break inside is a loop that stops from the middle, and is useful when the stopping test comes in the middle of the body. A sentinel is a special value, such as 0, that means stop.
A loop can ask the user again and again until the answer is right. A guessing game starts with a value that is not the secret, so that the loop begins, asks inside the loop, counts the guesses, and gives a hint. To check an answer, use while True with a break when the answer is good. A sentinel such as 0 ends a list of numbers. On this site, the answers go into the Input box before Run, one on each line, and a loop that asks many times needs as many lines as it asks, or the program stops with an EOFError.
Use a for loop when the number of rounds is known or when you go through the items of something, and use a while loop when the rounds depend on a condition. Many jobs can be done with either. The two usual faults of loops are the endless loop, where nothing in the body moves the condition towards False, and the off-by-one fault, where a loop makes one round too many or too few because of < and <=. To find them, follow the loop with a trace table, print the variable inside the loop, and test with a small number of rounds first.
In this project we write a number guessing game with hints and a limit of tries. We write the rules first, plan the loop with the conditions that end it (the player wins, or the tries are used up), build the game from the simple version of the last lesson, test the cases that end the game in different ways, and extend it with a random secret, a best score and a play-again question. The project uses a while loop with two reasons to stop, input(), if, elif and else, and a flag variable.
Log in to join the discussion.
Loading…