Lab 6: HiLo Game
This part of the lab gives you extra practice with while
loops.
In this part of today's lab, you'll implement a Hi/Lo guessing game.
Your program will pick a random number between 1 and 50, and the user is allowed 5 chances to guess the number. Your game should provide feedback on each guess, as shown in the example runs below.
Example 1: The user correctly guesses the number in less than 5 guesses
>>> hiLo()
Try to guess the number I am thinking of between 1 and 50. You have 5 chances.
Take a guess: 50
Too high...
Take a guess: 40
Too low...
Take a guess: 45
Wahooo! You guessed my secret number! and it only took you 3 guesses!
Example 2: The user runs of out guesses (and the number is revealed)
>>> hiLo()
Try to guess the number I am thinking of between 1 and 50. You have 5 chances.
Take a guess: 20
Too low...
Take a guess: 40
Too low...
Take a guess: 45
Too low...
Take a guess: 48
Too high...
Take a guess: 47
Too high...
Sorry, you have run out of guesses. The secret number was 46.
Task 1: Write the hiLo game
The provided hilo.py
file contains the complete function getUsersGuess()
and the start of the hiLo()
function, which you will complete.
There are different strategies for writing the code for this game. If you have an idea in your head, go with it! If you need some help getting started, consider the following...
Your program needs to handle these things:
- The secret random number between 1 and 50, inclusive
- How many guesses has the user taken (because only 5 are allowed)
- Is the user's guess too high, too low, or just right?
- The cases when the user guesses the secret number or runs out of guesses
It's useful to draw an iteration table to help visualize how the game unfolds, noting:
- What are the variables that your game needs to keep track of?
- Draw one row for each time the user guesses— what changes with each guess? What stays the same?
- When does the game stop?
Looking for a bit more guidance? Click here to follow our hiLo game walk-through...
Tips:
- Recall that the
break
statement can be used to end a loop. - Write your code in small segments, testing as you go. Your first goal should be to get something very small working, perhaps asking for the user's guess (and not yet checking if it is correct).
Task 2: Validate the user's input
Instead of assuming that the user will type in a positive integer (and not, say, a letter or a negative number), you can write code to check for that. A while loop works well.
What makes a valid guess?
- Guess is not a negative number.
- Guess is not beyond our max (50).
- Guess is a number, not a letter, symbol etc. Python's
isdigit()
function can be used to determine if a string contains only digits, so it will be helpful here.
Example use of isdigit()
:
ex1 = 'abc'
print(ex1.isdigit()) # False
ex2 = '123'
print(ex2.isdigit()) # True
When adding validation, you may have to write your own version of getUsersGuess()
because the given version breaks when the user enters a string. Note that the code below is pseudo-code (not
actual python, but code-like words that capture the essence behind the code).
# pseudo-code (not real python code, but close)
while input is not valid:
ask user for input
Here is what your program might look like after implementing input validation:
Try to guess the number I am thinking of between 1 and 50. You
have 5 chances.
Take a guess: -25
Sorry, that is not a valid input. Try again.
Take a guess: 800
Sorry, that is not a valid input. Try again.
Take a guess: help
Sorry, that is not a valid input. Try again.
Take a guess: ARGH
Sorry, that is not a valid input. Try again.
Take a guess: 0
Sorry, that is not a valid input. Try again.
Take a guess: 2000000
Sorry, that is not a valid input. Try again.
Take a guess: 30
Too low...
Table of Contents
- Lab 6 Home
- Part 2: Loops with Strings (
for
loops) - Part 3: HiLo Game (
while
loops) - Part 4: Loops and Graphics
- Knowledge Check