✕ Close
← Import a toolbox (random)

Level 5 · Lesson 41: Project: Guess the Number

Practice: little programs →
PROJECT
Guess the Number
The computer thinks of a number. You guess it.
press Enter ↵
🐍 main.py
Ctrl + Enter runs it too. Esc, then Tab, leaves the code.
Output

Press ▶ Run to see what your program does.

Lesson 41: Project: Guess the Number

Level 5, Python Thinks and Repeats. Build a game that gives hints until you win. The new word: game loop. It follows lesson 40, Import a toolbox (random). Next comes lesson 42, Practice: little programs.

What the lesson explains

Guess the Number. The computer thinks of a number. You guess it.

A game loop. The loop asks, checks and gives a hint until the guess is right. On the card: while guess != secret:

What you do first

Play it. Play until you win. One way to write it: secret = 7 / guess = 0 / while guess != secret: / guess = int(input("Guess? ")) / if guess < secret: / print("Too low") / elif guess > secret: / print("Too high") / print("You got it!"). Answer its questions with 5, 7. It prints: Guess? 5 / Too low / Guess? 7 / You got it!.

The code on the screen

A game loop. The editor shows 9 lines: secret = 7 / guess = 0 / while guess != secret: / guess = int(input("Guess? ")) / if guess < secret: / print("Too low") / elif guess > secret: / print("Too high") / print("You got it!"). Run it, and it prints "Guess? 3", "Too low", "Guess? 9", "Too high", "Guess? 7", "You got it!" (answering 3, 9, 7).

What you practise

Try it, "Play it": Play until you win. One way to write it: secret = 7 / guess = 0 / while guess != secret: / guess = int(input("Guess? ")) / if guess < secret: / print("Too low") / elif guess > secret: / print("Too high") / print("You got it!"). Answer its questions with 5, 7. It prints: Guess? 5 / Too low / Guess? 7 / You got it!.

Project, "A real secret": Use random for the secret. The editor opens with: secret = 7 / guess = 0 / while guess != secret: / guess = int(input("Guess? ")) / if guess < secret: / print("Too low") / elif guess > secret: / print("Too high") / print("You got it!"). One way to write it: import random / secret = random.randint(1, 10) / guess = 0 / while guess != secret: / guess = int(input("Guess? ")) / if guess < secret: / print("Too low") / elif guess > secret: / print("Too high") / print("You got it!"). Answer its questions with 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. It prints: Guess? 1 / You got it!.

What PlayBuddy and PlayType say

PlayBuddy: "Project time! Let’s build a guess the number game."

PlayType: "Too high? Too low? The game gives hints until you win."

Help in every lesson

Everything happens on a pretend computer in your browser, so nothing touches your real files. PlayBuddy and PlayType stand beside the board, cheer you on and say when it is your turn.

A green ring shows where to click the first time. After that you do it on your own, and if you slip twice the ring comes back.

Stars

The lesson has 2 things to do. Up to one slip in ten gives three stars, up to one in four gives two, and finishing always gives at least one. Stars and progress are saved in this browser only; there is no account.

About DayOneCode

DayOneCode is an independent learning site, not affiliated with or endorsed by Anthropic. Claude and Claude Code are trademarks of Anthropic.