hands-on labIDE in your browser

The Guessing Game: random and Loops That Count

Sam's counter game: guess how many beans are in the jar. Pick a random number, loop until the guess is right with higher and lower hints, count the guesses, stop after seven, and say when a guess is close.

time
25 min
checked steps
5
level
Beginner
setup
None

$29.99a month with Pro

Pro opens all 5 steps of this lab and every other lab. Cancel any time.

no account needed · Pro opens all 5 steps

See Pro plans

The job

A jar of coffee beans sits on the counter: guess how many and your coffee is free. Sam wants a version on the till for slow afternoons. You write it, and you play it in the terminal as you go.

5 steps, each checked when you finish it

A check runs your work at the end of every step. Hints and the full solution are there if you get stuck.

  1. 1

    Beans in the jar

    Python comes with a library of ready-made tools, grouped into modules.

    read this step below ↓

  2. 2

    Until you get it

    One guess makes a short game.

  3. 3

    Count the guesses

    Sam's regulars want bragging rights.

  4. 4

    Seven tries

    A game you can't lose isn't much of a game.

  5. 5

    But close!

    Players near the answer want to know it.

Step 1 as it appears in the lab

The lab’s own text. The hint and the solution stay inside the lab.

Step 1: Beans in the jar

Python comes with a library of ready-made tools, grouped into modules. import random at the top of game.py brings in the one for chance, and random.randint(1, 100) picks a whole number from 1 to 100, both ends included. Every Run fills the jar differently.

Ask for one guess and say how it went:

Guess: 50
Too high

Too low when the guess is below the number of beans, Too high when it's above, Exactly! when it's the number. That's an if, an elif and an else, the Level 2 kind.

Press Run and type a guess in the terminal. You can't know the answer, and neither could the check, so it sets beans to a number it picks before it plays.

game.py, the file you edit6 lines
# Sam's jar game: how many coffee beans are in the jar?
import random

beans = random.randint(1, 100)   # Sam fills the jar: a whole number from 1 to 100

# TODO (Step 1): ask "Guess: ", turn it into a number, and print Too low, Too high or Exactly!

what you get

  • An editor and terminal in your browser
  • A check at the end of every step
  • Hints and the full solution
  • Nothing to install or configure

before you start

  • The Order Queue: while loops, break

Part of the Learn Python track

Lab 12 of 15. Learn Python by running a café till: receipts, orders, discounts and the Friday rush. Nothing to install.

Questions about this lab

Does random.randint include both ends?

Yes. random.randint(1, 100) can give 1, 100 or anything in between.

Do I need to install anything?

No. Python runs right in your browser, and you type your guesses in the lab's terminal.

What you do in this lab

You import Python's random module and build a guessing game step by step: a random number, a loop that gives higher and lower hints until the guess is right, a counter for the guesses, a limit of seven tries and a hint when a guess is close. The check fixes the number so it knows the answer.