The Task
Build a program that:
- Picks a random number between 1 and 100.
- Asks the user to guess the number.
- Tells the user if their guess is too high, too low, or correct.
- Keeps asking until the user guesses correctly.
- Counts and prints how many guesses it took.
A Quick New Thing: import
We need a random number for this project, so use the following:
import random
number = random.randint(1, 100)This picks a random whole number between 1 and 100, including both numbers.
Don’t worry about understanding import or random yet. We’ll learn about modules and import later.
For now, just use these two lines to generate the random number.
What You’ll Use
import random and random.randint()to pick the numberinput()andint()to take each guess- Comparison operators (
<,>,==) if,elif,elseto respond to the guess- A
while Trueloop withbreakto keep asking until the correct guess - A counter variable to track the number of guesses
Expected Output
Guess the number between 1 and 100: 50
Too high! Try again.
Guess the number between 1 and 100: 25
Too low! Try again.
Guess the number between 1 and 100: 37
Correct! You guessed it in 3 tries.Try It Yourself First
Here are a few hints:
while Truecombined withbreakworks well here because you don’t know in advance how many guesses it will take.- Start a counter at
0before the loop, and add1to it on every guess. - Compare the guess to the number using
if/elif/else, just like we did with the Grade Calculator. - Use
breakonly when the guess is correct.
Give it a try before you scroll down.
Solution
import random
number = random.randint(1, 100)
guesses = 0
while True:
guess = int(input("Guess the number between 1 and 100: "))
guesses += 1
if guess < number:
print("Too low! Try again.")
elif guess > number:
print("Too high! Try again.")
else:
print("Correct! You guessed it in", guesses, "tries.")
breakHow It Works
random.randint(1, 100)picks the number once, before the loop starts, so it stays the same for the whole game.guessesstarts at0and increases by1on every guess using+=from the operators lesson.while Truekeeps the loop running untilbreakstops it.- The
if/elif/elseblock checks the guess against the random number and gives the user feedback. breakonly runs inside theelseblock because that’s the only case where the guess is correct.
Common Mistake
Be careful where you place break.
For example:
while True:
guess = int(input("Guess the number between 1 and 100: "))
guesses += 1
if guess < number:
print("Too low! Try again.")
elif guess > number:
print("Too high! Try again.")
else:
print("Correct!")
breakThis stops the loop after the first guess, whether the guess is correct or not.
Because break is now outside the else block.
It runs every time the loop reaches that line.
The correct version keeps break inside else:
else:
print("Correct!")
breakIndentation decides which block a line belongs to, just like we saw in the nested if lesson.
Challenges
If you want to go further, try these:
- Give the player a maximum of
10guesses. If they use all10guesses without finding the number, end the game. - If the player runs out of guesses, show them the correct number.
👉 Next tutorial: Python Functions