Build a simple calculator using Python in this beginner-friendly mini project. Learn how to implement basic arithmetic operations like addition, subtraction, multiplication, and division.
Objective: To create a simple “Guess the Number” game in Python using a while
loop, demonstrating core programming concepts such as random number generation, user input, conditional statements (if-elif-else
), and iteration.
Target Audience: Students learning introductory Python programming.
Estimated Time: 1-2 hours (can be extended with enhancements)
Students will write a Python program where the computer “thinks” of a secret number within a specified range. The player then tries to guess this number. The program will provide helpful hints (e.g., “Too high!” or “Too low!”) after each guess, guiding the player until they guess correctly. A while
loop will be the key to keeping the game running until the correct guess is made.
Upon completion of this project, students will be able to:
random
module to generate random integers.input()
and int()
.while
loops for repetitive tasks.if-elif-else
statements for decision-making.print()
.Step 1: The Computer Chooses a Secret Number
Import the random
module: This module provides functions for generating random numbers.
Generate a secret number: Use random.randint(low, high)
to pick a random integer within a desired range (e.g., between 1 and 100, inclusive). Store this number in a variable (e.g., secret_number
).
import random
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
# Optional: For testing purposes, you can uncomment the line below to see the secret number
# print(f"DEBUG: The secret number is {secret_number}")
Step 2: Set Up the Game Variables
Initialize num_guesses
: This variable will keep track of how many attempts the player makes. Start it at 0
.
Initialize player_guess
: This variable will store the player’s current guess. Initialize it to a value that cannot be the secret number (e.g., 0
or None
) to ensure the while
loop starts correctly.
num_guesses = 0
player_guess = 0 # Initialize to a value that won't match secret_number initially
Step 3: Implement the Game Loop (while
loop)
Create the while
loop: The loop should continue executing as long as player_guess
is not equal to secret_number
.
Inside the loop, get user input:
input()
.input()
is always a string. Use int()
to convert it to a number so you can compare it with secret_number
.int()
conversion will cause an error (ValueError
). For now, just have them re-run, or introduce try-except
(see enhancements).Increment num_guesses
: After each valid guess, increase the num_guesses
counter.
Provide hints using if-elif-else
:
player_guess
is greater than secret_number
, print “Too high! Try again.”player_guess
is less than secret_number
, print “Too low! Try again.”while
loop condition (player_guess != secret_number
) takes care of the “correct guess” scenario. When the guess is correct, the loop will simply terminate.while player_guess != secret_number:
try:
player_guess_str = input("Guess the number (between 1 and 100): ")
player_guess = int(player_guess_str)
num_guesses += 1 # Increment guess count only for valid number input
if player_guess > secret_number:
print("Too high! Try again.")
elif player_guess < secret_number:
print("Too low! Try again.")
# If player_guess == secret_number, the loop condition becomes false and the loop exits.
except ValueError:
print("Invalid input. Please enter a whole number.")
# Do NOT increment num_guesses here as it wasn't a valid attempt
Step 4: Winning Message
Outside the loop: Once the while
loop condition (player_guess != secret_number
) becomes False
(meaning player_guess
is equal to secret_number
), the code will continue directly after the loop.
Congratulate the player: Print a message announcing their victory.
Display total guesses: Use an f-string to clearly show how many attempts it took.
print(f"Congratulations! You guessed the number {secret_number} in {num_guesses} guesses!")
import random
# Step 1: The Computer Chooses a Secret Number
secret_number = random.randint(1, 100)
# print(f"DEBUG: The secret number is {secret_number}") # For testing
# Step 2: Set Up the Game Variables
num_guesses = 0
player_guess = 0 # Initialize to a value that won't match secret_number initially
print("Welcome to the Guess the Number Game!")
print("I'm thinking of a number between 1 and 100.")
# Step 3: Implement the Game Loop (while loop)
while player_guess != secret_number:
try:
player_guess_str = input("Enter your guess: ")
player_guess = int(player_guess_str)
num_guesses += 1 # Increment guess count for valid number input
if player_guess > secret_number:
print("Too high! Try again.")
elif player_guess < secret_number:
print("Too low! Try again.")
# No 'else' needed here, as if it's not high or low, it must be correct,
# and the loop condition will then be false, causing it to terminate.
except ValueError:
print("Invalid input. Please enter a whole number.")
# If input is not a number, we don't count it as a guess and the loop continues.
# Step 4: Winning Message (This code runs AFTER the loop finishes)
print(f"Congratulations! You guessed the number {secret_number} in {num_guesses} guesses!")
print("Thanks for playing!")
try-except ValueError
to handle non-integer input gracefully, perhaps in a small helper function.max_guesses
variable (e.g., 7).while
loop condition to also check num_guesses < max_guesses
.secret_number
.random.randint()
or different max_guesses
.while
loop.input("Do you want to play again? (yes/no): ")
.num_guesses
in a list or even save it to a text file.while
loop suitable for this game?”random.randint()
do?”int(input(...))
?”if-elif-else
structure help the player?”Tutorials, Roadmaps, Bootcamps & Visualization Projects