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)returnsTrueorFalse. - 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:
get_range()accepts no arguments.- Ask the user for a maximum number to guess. (To guess between 1 and 100, the
user enters
100.) - Cast the user's entry to an
int. - Store the int in a variable called
top. - Don't allow an illegal number — loop steps 2–3 while the number entered is less than 1.
- Return
top.
Create a get_guess Function
This function asks the user to guess a number. Name it get_guess:
get_guess(top)accepts one argument: the maximum number in the range.- Ask the user to guess a number between 1 and
top. - Cast the entry to an
int. - Store the int in a variable called
guess. - Don't allow an illegal number — loop steps 2–3 while the number is less than
1 or greater than
top. - Return
guess.
Create a check_guess Function
Name this function check_guess:
check_guess(guess, secret)accepts two arguments: a guessed number and the secret number.- Check whether
guessequalssecret. ReturnTrueif it does,Falseif 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:
Get a random integer between 1 and 10 like this:
Now write the main program:
- Say
Welcome to Guess the Number! - Call
get_range()and store its result. - Call
random.randint()to get a secret number in the range the user asked for (pass it1and the result of step 2). Store it. - Call
get_guess()and store its result. - Call
check_guess(), passing it the secret number (step 3) and the guess (step 4). - If
check_guess()returnedFalse, sayNope! - Loop steps 4, 5, and 6 until
check_guess()returnsTrue. - 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. |