Day 005 of the 100 Days of Python challenge is a terminal Number Guessing Game. Python chooses a secret number from 1 to 100, and the player keeps guessing until they find it or quit.
This project introduces while loops — the tool you reach for when you don’t know in advance how many times something needs to repeat. Unlike the for loops used in earlier days, which run a fixed number of times, this game needs to keep asking for guesses indefinitely, until the player either guesses correctly or chooses to quit. It also pairs that loop with try/except error handling, so a single bad input doesn’t crash the whole game.
This guide walks through the complete Day 005 source code and explains how the loop, the comparisons, and the error handling work together.
Project Overview
The secret number is generated once, before the game loop starts:
secret_number = random.randint(1, 100)
A while True loop keeps the game running indefinitely. Each guess is checked for the special q command, converted to an integer inside a try block, and compared with the secret number. The program reports whether the guess is too low or too high, and an attempts counter increases only after a valid numeric guess.
Source Code
The Day 005 project is contained in day005/main.py in the 100daysofpython repository:
import random
trophy = """
* * * * * * * * * * * * * * *
* ___________ *
* '._==_==_=_.' *
* .-\: /-. *
* | (|:. |) | *
* '-|:. |-' *
* \::. / *
* '::. .' *
* ) ( *
* _.' '._ *
* * * * * * * * * * * * * * *
"""
def number_guessing_game():
secret_number = random.randint(1, 100)
attempts = 0
print("Welcome to the Number Guessing Game!")
print("Pick a number between 1 and 100. Try to guess it!")
while True:
print("**********************************************")
guess = input("Enter your guess (or 'q' to quit): ")
if guess.lower() == 'q':
print(f"Thanks for playing! The number was {secret_number}.")
break
try:
guess = int(guess)
attempts += 1
if guess < secret_number:
print(" - Your guessed number is too low! Try again.")
elif guess > secret_number:
print(" - Your guessed number is too high! Try again.")
else:
print(f" - Congratulations! You guessed it in {attempts} attempts.")
print(trophy)
break
except ValueError:
print(" - Please enter a valid number or 'q' to quit.")
if __name__ == "__main__":
number_guessing_game()
How the Program Works
An Infinite Loop With Two Exits
while True:
...
while True creates a loop that would normally run forever, since its condition is always true. The loop only ends through an explicit break statement — and this game has two of them: one when the player types q to quit, and one when the player guesses correctly. Every other outcome loops back to ask for another guess.
Letting the Player Quit at Any Time
if guess.lower() == 'q':
print(f"Thanks for playing! The number was {secret_number}.")
break
Checking for 'q' before trying to convert the guess to a number means quitting never risks crashing the program — this check happens first, before any numeric conversion is attempted.
Handling Invalid Input With try/except
try:
guess = int(guess)
...
except ValueError:
print(" - Please enter a valid number or 'q' to quit.")
If the player types something that isn’t a number and isn’t 'q' — like "banana" — int(guess) raises a ValueError. Instead of letting that error crash the program, the except ValueError: block catches it and prints a helpful message, and the loop simply asks for another guess. This is a much friendlier alternative to validating input with .isdigit() beforehand, since it lets Python’s own error handling do the work.
Tracking Attempts and Comparing the Guess
attempts += 1
if guess < secret_number:
print(" - Your guessed number is too low! Try again.")
elif guess > secret_number:
print(" - Your guessed number is too high! Try again.")
else:
print(f" - Congratulations! You guessed it in {attempts} attempts.")
print(trophy)
break
Notice that attempts += 1 only runs inside the try block, after a successful conversion — so invalid guesses like "banana" don’t count against the player’s attempt total. Only real numeric guesses increase the counter.
What This Project Teaches
Day 005 combines:
whileloops for repeating an unknown number of timesbreakfor exiting a loop from multiple different conditionstry/exceptfor handling invalid input gracefully- A counter variable that updates conditionally, not on every loop iteration
- Comparison operators for giving the player “too high”/“too low” feedback
Running the Project
git clone https://github.com/victorhugo81/100daysofpython.git
cd 100daysofpython/day005
python main.py
Possible Next Steps
Once you understand the existing code, try extending it:
- Limit the player to a fixed number of attempts, and end the game if they run out
- Add difficulty levels that change the number range (e.g., 1–50 for easy, 1–500 for hard)
- Track and display the best (lowest-attempt) score across multiple games using a loop around
number_guessing_game() - Give a “getting warmer/colder” hint based on how close the previous guess was
Conclusion
Day 005 of the 100 Days of Python challenge shows how a while True loop, combined with break and try/except, can build a genuinely playable game that handles both correct answers and bad input gracefully. The pattern of looping until an explicit exit condition is met — rather than a fixed number of times — is one you’ll use constantly once you start building programs that respond to real user behavior.
View the Day 005 source code on GitHub, then continue through the repository’s project index as you build more Python programs, one day and one small project at a time.
