>_Code Club
← All exercises

python

Game: guess the number

Time to build a game! The computer picks a secret number between 1 and 100, and the player tries to guess it. After each guess, the computer says too big or too small.

Two new tools:

import random
secret = random.randint(1, 100)   # a random whole number from 1 to 100

answer = input("Your guess: ")    # waits for the player to type something
guess = int(answer)               # input() gives text: turn it into a number

When your program calls input(), the terminal waits for you to type an answer and press Enter.

Your mission

There's no automatic check for this one: it's yours to play with! Ideas once it works:

  • Count how many tries the player needed.
  • Ask "Play again?" at the end.
  • Add difficulty levels (1–10, 1–100, 1–1000).
  • What happens if the player types banana? Can you handle it? (Look up try / except.)
Loading the editor…