1) What do I need to do?
π― Your mission: Build the full game!
Make the game keep going until the player guesses right:
β’ random.randint for the secret β’ a while loop β’ int(input()) for guesses
β’ Say Too high! or Too low! β’ Count the tries and print them at the end
2) What do I need to know?
Plan the game
A villain hid a secret number between 1 and 20. The hero has to guess it!
The plan:
1. Pick a random secret number
2. Loop while the guess is wrong
3. Ask for a guess (as an int)
4. Say too high or too low
5. When it's right, celebrate and say how many tries it took
Here's a start β it only lets you guess once. Your job is to make it a real game!
π§° Your toolkit for this mission
Everything you need to know to finish this lesson on your own:
The secret
The computer picks a random number for the player to find.
How to write it: secret = random.randint(1, 20) after import random.
import random secret = random.randint(1, 20)
Number guesses
Guesses must be integers to compare with the secret.
How to write it: guess = int(input("Guess: ")).
Game loop
Repeat until the guess is right.
How to write it: while guess != secret: β inside, give a clue and ask again.
while guess != secret:
if guess < secret:
print("Too low!")
else:
print("Too high!")
guess = int(input("Again: "))Win message
Print it after the loop ends.
How to write it: Not indented, so it runs once.
print("You got it!")Think about it
Look at this code. What happens next, and why? Say it in your own words.
guess = 7 print(guess == secret)
Watch & explore βΆ
Power-up: Plan the game
See it happen
1 / 3secret = 7 guess = 0
Plan a hidden number and a starting guess. Later, make the secret random.
3) Run the code & terminal output
Press βΆ RUN and your program's messages appear here!


Captain Py
Our next adventure is yours to create.
