We build the game
Section 8: The project: the guess-the-number gameLesson 8.2 · ⏱ 12 min
import random
secret = random.randint(1, 20)
tries = 0
while True:
guess = int(input("Guess 1 to 20: "))
tries = tries + 1
if guess == secret:
print("Correct! 🎉 in", tries, "tries")
break
elif guess < secret:
print("Higher ⬆")
else:
print("Lower ⬇")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Guess 1 to 20: 10 Higher ⬆ Guess 1 to 20: 15 Lower ⬇ Guess 1 to 20: 12 Higher ⬆ Guess 1 to 20: 13 Higher ⬆ Guess 1 to 20: 14 Correct! 🎉 in 5 tries
🖥️ What happens on the stage?
Try it: the best way is to start with 10, then halve the distance each time.
📌 Another example: the game with numbers up to 20
A bigger range (1 to 20). random.seed makes the secret number the same every time, so we can test.
When the program asks, we type: 10, then 5, then 8.
import random
random.seed(3)
secret = random.randint(1, 20)
tries = 0
while True:
guess = int(input("Guess 1 to 20: "))
tries = tries + 1
if guess < secret:
print("higher")
elif guess > secret:
print("lower")
else:
print("Yes! in", tries, "tries")
break
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Guess 1 to 20: 10 lower Guess 1 to 20: 5 higher Guess 1 to 20: 8 Yes! in 3 tries
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
The game says how many tries are left (only 5 tries).
👀 Show the solution🙈 Hide the solution
When the program asks, we type: 1, then 8.
import random
random.seed(3)
secret = random.randint(1, 20)
for left in range(5, 0, -1):
guess = int(input("Guess: "))
if guess == secret:
print("You win!")
break
print("No.", left - 1, "tries left")
else:
print("Game over. It was", secret)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Guess: 1 No. 4 tries left Guess: 8 You win!
Result: a wrong try, then You win!
Medium
The other way round: the computer guesses your number (you type higher or lower).
👀 Show the solution🙈 Hide the solution
When the program asks, we type: higher, then lower, then yes.
low, high = 1, 100
while True:
guess = (low + high) // 2
a = input("Is it " + str(guess) + "? (yes/higher/lower) ")
if a == "yes":
print("I found it!")
break
elif a == "higher":
low = guess + 1
else:
high = guess - 1
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Is it 50? (yes/higher/lower) higher Is it 75? (yes/higher/lower) lower Is it 62? (yes/higher/lower) yes I found it!
Result: it finds the number in 3 tries.
Hard
Add the word hot if the guess is close (a difference of 2 or less).
👀 Show the solution🙈 Hide the solution
When the program asks, we type: 9, then 8.
import random
random.seed(3)
secret = random.randint(1, 20)
while True:
guess = int(input("Guess: "))
if guess == secret:
print("Yes!")
break
if abs(guess - secret) <= 2:
print("hot!")
elif guess < secret:
print("higher")
else:
print("lower")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Guess: 9 hot! Guess: 8 Yes!
Result: hot!, then Yes!

