Skip to content

Guess the Number

You'll build a game where the player hunts for a secret random number. The trick is to keep the main program short and readable by pushing the fiddly details — getting valid input, checking the guess — into their own functions.


Overview

You'll write three helper functions and a main program that ties them together into a working guessing game.

You've got it when…

  • get_range() asks the user for the top of the range and rejects bad input.
  • get_guess(top) asks for a guess and rejects out-of-range input.
  • check_guess(guess, secret) returns True or False.
  • The game loops until the player guesses the secret number.

Collaboration & AI

Work: On your own. Compare approaches with a neighbor, but write your own functions.

AI — AIAS Level 1, No AI: Design the functions and the loop yourself. What the levels mean.


Create a get_range Function

This function sets up the game by asking the user for the range of numbers to guess from. Name it get_range with this behavior:

  1. get_range() accepts no arguments.
  2. Ask the user for a maximum number to guess. (To guess between 1 and 100, the user enters 100.)
  3. Cast the user's entry to an int.
  4. Store the int in a variable called top.
  5. Don't allow an illegal number — loop steps 2–3 while the number entered is less than 1.
  6. Return top.

Create a get_guess Function

This function asks the user to guess a number. Name it get_guess:

  1. get_guess(top) accepts one argument: the maximum number in the range.
  2. Ask the user to guess a number between 1 and top.
  3. Cast the entry to an int.
  4. Store the int in a variable called guess.
  5. Don't allow an illegal number — loop steps 2–3 while the number is less than 1 or greater than top.
  6. Return guess.

Create a check_guess Function

Name this function check_guess:

  1. check_guess(guess, secret) accepts two arguments: a guessed number and the secret number.
  2. Check whether guess equals secret. Return True if it does, False if it doesn't.

The Main Program

Python's random module makes random selections. We'll use it for the secret number. Put this at the top of your file:

import random

Get a random integer between 1 and 10 like this:

secret_number = random.randint(1, 10)

Now write the main program:

  1. Say Welcome to Guess the Number!
  2. Call get_range() and store its result.
  3. Call random.randint() to get a secret number in the range the user asked for (pass it 1 and the result of step 2). Store it.
  4. Call get_guess() and store its result.
  5. Call check_guess(), passing it the secret number (step 3) and the guess (step 4).
  6. If check_guess() returned False, say Nope!
  7. Loop steps 4, 5, and 6 until check_guess() returns True.
  8. Print You guessed correctly!

Turn It In

  • Your Python (.py) file.

How It's Graded

This lab is worth up to 4 points. One score covers everything you turn in.

Score What it looks like
4 — Excellent All three functions match their specs — get_range() and get_guess(top) loop until they get a legal number, check_guess(guess, secret) returns True or False — and the main program picks its secret with random.randint, loops until the guess lands, and says so. Bad input can't break the game; you tried.
3 — Above Average The game plays start to finish, with a slip — one validation hole (say, a 0 sneaks through), or a function that works but strays from its spec.
2 — Average The game only works when the player behaves — validation missing — or one of the three functions is absent, with its job inlined into the main program.
1 — Below Average The game can't be played to a win, or the program doesn't run.
0 — Failing Nothing submitted, or no evidence of the game.