Understanding for Loops
if guess == number: print 'Congratulations, you guessed it.' # this causes the while loop to stop running = False elif guess < number: print 'No, it is a little higher than that.' else : print 'No, it is a little lower than that.' else : print 'The while loop is over.' # Do anything else you want to do here print 'Done'
The Loop Behind the Guessing Game
The source example is presented under the topic of for loops, but the mechanism it demonstrates is a while loop. The program repeatedly asks for an integer, compares the guess with a target number, and gives feedback. It keeps repeating while the variable running is True. When the guess equals the target, the program prints a congratulatory message and changes running to False.
The central idea is repetition controlled by a condition: check running, execute the loop block, check running again, and either repeat or continue after the loop.
One Complete Loop Cycle
Before the loop block runs, the program checks whether running is True. If it is, the block executes. The block obtains a guess and uses conditional statements to compare that guess with number. After the block finishes, the loop condition is checked again. If running is still True, another iteration begins. If running has become False, the loop ends and its optional else block can run.
Following the Guess Sequence
Three Attempts
Trace the source example when the entered guesses are 50, 22, and 23.
First attempt: 50: The guess is higher than the target. The program prints, "No, it is a little lower than that." The loop continues.
Second attempt: 22: The guess is lower than the target. The program prints, "No, it is a little higher than that." The loop continues.
Third attempt: 23: The guess equals the target. The program prints, "Congratulations, you guessed it." The running variable is changed to False.
After the loop: Because the loop condition is now False, the while loop ends. Its else block prints, "The while loop is over," and the next statement prints, "Done."
The output order is: the lower message, the higher message, the congratulations message, the while-loop-over message, and Done.
Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
DoneChoosing the Feedback Branch
The first test asks whether guess == number. If that comparison is true, the program prints the congratulations message and makes the loop stop. If the first test is false, the elif test asks whether guess < number. A true result produces the message that the target is a little higher. If neither comparison is true, the final else produces the message that the target is a little lower.
| Comparison result | Selected branch | Message or action |
|---|---|---|
| guess == number | if | Print congratulations and make running False |
| guess < number | elif | Print that the target is a little higher |
| Neither comparison is true | else | Print that the target is a little lower |
The conditional branches used inside the loop
What Happens After Repetition
The while loop has an optional else block. In this example, that else block prints "The while loop is over." It runs when the while-loop condition becomes False. The source also explains that the else block is always executed unless the loop is exited with a break statement. In the demonstrated path, the condition becomes False because the successful guess changes running to False, so the else block runs before the next statement prints "Done."
Mistakes in Loop Tracing
Assuming the loop ends immediately after a comparison.
A comparison selects a branch, but repetition is controlled by the running condition.
Fix:
After each loop block, check the value of running again. The loop repeats while it is True.Forgetting that the successful branch changes the loop state.
If running stays True, the loop condition remains true and another iteration can begin.
Fix:
On the successful comparison, track the assignment that changes running to False.Reversing the higher and lower feedback.
The source sequence uses 22 as a lower guess and therefore prints that the target is a little higher.
Fix:
For guess < number, select the message saying the target is a little higher. Otherwise, after the equality test fails, select the message saying the target is a little lower.Expecting the while-loop else block after every exit.
The source states that the else block is skipped when break exits the loop.
Fix:
Distinguish normal termination through a False condition from termination caused by break.
A Reliable Tracing Method
- Write down the current value of running before the loop iteration begins.
- Record the current guess and compare it with number.
- Identify which of if, elif, or else supplies the message.
- Check whether the successful branch changes running to False.
- If running is still True, trace the next guess; otherwise, trace the while-loop else block and then the following statement.
Using the same control flow, explain what happens when the first guess is 22 and the second guess is 23. Identify the message from each attempt, the value of running after the successful guess, and whether the while-loop else block runs.
Hints
- Compare 22 with number and select the elif or else branch according to the source sequence.
- The successful guess changes running to False.
- A False loop condition causes the while-loop else block to execute.
Key Takeaways
- The source example uses a while loop controlled by the variable running.
- The loop checks its condition before each iteration and checks it again after the loop block finishes.
- The if, elif, and else branches choose feedback based on the relationship between guess and number.
- A correct guess changes running to False, which ends repetition.
- The while-loop else block runs when the condition becomes False, but not when break exits the loop.
Key Takeaways
- A while loop repeats its block while its condition remains True.
- The guessing example compares each guess with a target number and selects one of three feedback branches.
- The successful branch changes running to False, allowing the loop to finish.
- The loop's else block runs after normal termination through a False condition, but not after break.