Guess the Number Game
BasicBuild an interactive guessing game in Python
1. Project Overview
This is a simple game where the computer picks a secret number, and you try to guess it. After each guess, the program tells you if your guess is too high, too low, or correct. It also counts how many tries you take to win.
2. Learning Objectives
By completing this project, students will learn:
- How to use variables to store data (the secret number, number of attempts).
- How to get input from the user and convert it to numbers.
- How to use if, elif, else for decision making.
- How to use a while loop to repeat actions until a goal is met.
- How to handle errors with try/except (for invalid input).
- How to use the random module to generate a random number.
3. Step-by-Step Explanation
- Plan the game
- The computer thinks of a number between 1 and 100.
- You keep guessing until you find the number.
- The program tells you "Too high" or "Too low" each time.
- It counts your attempts and congratulates you at the end.
- Import what you need
- Use the random module to get a random number.
- Welcome the player
- Print friendly instructions so the player knows what to do.
- Pick the secret number
- Use
random.randint(1, 100)to choose a number from 1 to 100.
- Use
- Start a loop to ask for guesses
- Use
while Trueto keep asking until the guess is correct.
- Use
- Read and check the user's input
- Get input as text.
- Try to convert it to an integer.
- If it's not a number, show a helpful message and ask again.
- If it's outside 1–100, ask again.
- Compare the guess
- If the guess is too low, say so.
- If the guess is too high, say so.
- If it's correct, celebrate and stop the loop.
- Count attempts
- Increase attempts by 1 each time a valid guess is made.
4. Complete and well-commented Python code
Copy and run this code:
import random # We will use this to pick a random number
print("Welcome to Guess the Number!")
print("I'm thinking of a number between 1 and 100.")
print("Try to guess it!")
# Choose a secret number between 1 and 100 (both included)
secret_number = random.randint(1, 100)
attempts = 0 # This will count how many valid guesses the player makes
while True:
# Ask the player for a guess
guess_text = input("Enter your guess (1-100): ")
# Try to convert the input to an integer
try:
guess = int(guess_text)
except ValueError:
# The player did not type a whole number
print("That's not a whole number. Please enter a number like 7 or 42.")
continue # Go back to the start of the loop and ask again
# Check that the guess is inside the valid range
if guess < 1 or guess > 100:
print("Your guess must be between 1 and 100. Try again.")
continue
# At this point, the guess is a valid number in range
attempts += 1
# Give hints or finish the game
if guess < secret_number:
print("Too low! Try a bigger number.")
elif guess > secret_number:
print("Too high! Try a smaller number.")
else:
# The player guessed correctly
print("Correct! You guessed the number!")
print("Number of attempts:", attempts)
print("Thanks for playing!")
break # Exit the loop because the game is won
5. Output examples
Example run 1:
I'm thinking of a number between 1 and 100.
Try to guess it!
Enter your guess (1-100): 50
Too low! Try a bigger number.
Enter your guess (1-100): 75
Too high! Try a smaller number.
Enter your guess (1-100): 62
Too low! Try a bigger number.
Enter your guess (1-100): 68
Correct! You guessed the number!
Number of attempts: 4
Thanks for playing!
Example run 2 (invalid input and out-of-range):
I'm thinking of a number between 1 and 100.
Try to guess it!
Enter your guess (1-100): hello
That's not a whole number. Please enter a number like 7 or 42.
Enter your guess (1-100): 150
Your guess must be between 1 and 100. Try again.
Enter your guess (1-100): 83
Too high! Try a smaller number.
Enter your guess (1-100): 72
Correct! You guessed the number!
Number of attempts: 2
Thanks for playing!
6. Small Exercise
Add a limit to the number of attempts
For example, the player only gets 7 tries to guess the number.
- Hint 1: Create a variable
max_attempts = 7. - Hint 2: After you increase attempts, check if
attempts == max_attemptsand the guess is still wrong. - Hint 3: If they run out of tries, reveal the secret number and end the game.
7. Summary
Great job! You built a complete interactive game using variables, loops, decisions, and input handling. These are the core skills of programming. Keep experimenting—small changes can make big improvements. You're on your way to creating your own fun programs!