انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

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:
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
🐍 Open the code in Robo Python ↗
🖥️ 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.

🐍 The game with numbers up to 20
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:
Guess 1 to 20: 10
lower
Guess 1 to 20: 5
higher
Guess 1 to 20: 8
Yes! in 3 tries
🐍 Open the code in Robo Python ↗
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

The game says how many tries are left (only 5 tries).

👀 Show the solution

When the program asks, we type: 1, then 8.

🐍 Solution
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:
Guess: 1
No. 4 tries left
Guess: 8
You win!
🐍 Open the code in Robo Python ↗

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

When the program asks, we type: higher, then lower, then yes.

🐍 Solution
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:
Is it 50? (yes/higher/lower) higher
Is it 75? (yes/higher/lower) lower
Is it 62? (yes/higher/lower) yes
I found it!
🐍 Open the code in Robo Python ↗

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

When the program asks, we type: 9, then 8.

🐍 Solution
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:
Guess: 9
hot!
Guess: 8
Yes!
🐍 Open the code in Robo Python ↗

Result: hot!, then Yes!

Finished the lesson? Mark it so your progress counts.