Python 5.8: Limit the number of lives: for loop
In this video we rewrite the magic number game with a for loop instead of a while loop. The behaviour we want to keep is important: when the player types something that is not a valid number, or a value outside the min/max range, the number of lives must not decrease. Only valid guesses should consume a life. We re-run the previous code first to confirm this is exactly what happens, then start refactoring.
From while to for with a break
We comment out the old loop and replace it with a for loop over range(number_of_lives). The current life counter becomes lives = number_of_lives - i. When the player wins, we want to leave the loop immediately, so we use break. The first naive version still prints "you lost" at the end even when the player wins, because the code that follows the loop runs in both cases.
The fix is a small boolean flag. We declare winning = False before the loop, set it to True in the winning branch right before the break, and after the loop we display the losing message only when winning is still False. Running the code again, a successful guess now prints the bravo message and nothing else; a series of wrong guesses correctly prints the losing message.
winning = False
for i in range(number_of_lives):
lives = number_of_lives - i
# ... ask the number and compare ...
if number == magic_number:
winning = True
break
if winning == False:
print("You lost, the magic number was", magic_number)
We now have two equivalent versions of the game: one with a while loop and one with a for loop, both behaving the same way. In the next section we will move on to a new project around small math games.
Summary
This lesson demonstrates how to limit the number of lives in a game using a for loop in Python. The tutorial validates user input against a defined range and decrements lives when incorrect answers are provided. It compares the for loop approach with a while loop implementation and introduces the break statement to exit the loop when the player wins.
Key points
- Using a for loop to control the number of attempts (lives) in a game
- Validating user input to ensure it falls within a specified range
- Decrementing the lives counter only when an incorrect or out-of-range answer is provided
- Using the break statement to exit the loop early if the player wins
- Displaying appropriate win/loss messages based on game outcome
- Comparing implementations: for loop versus while loop approaches
FAQ
Why use a for loop to limit lives instead of a while loop?
A for loop with a predefined number of iterations is ideal when you know exactly how many attempts a player should have. It's often clearer and more straightforward for controlling a fixed number of lives.
What does the break statement do in this game context?
The break statement allows you to exit the for loop early if the player wins before exhausting all lives, preventing unnecessary iterations and improving program efficiency.
How do you validate that user input is within the correct range?
Use conditional statements (if/else) to check if the input falls within the defined minimum and maximum values. Only decrement lives if the input is invalid or out of range.