Lab 5: Part 2 - While Loops

This part of the lab is not graded but it's great preparation if you want to work on the while loops project this week and is also great extra while loop practice if you're not doing that task.

In this part of today's lab, you'll implement a Hi/Lo guessing game, which demonstrates the usefulness of while loops.

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:

It's useful to draw an iteration table to help visualize how the game unfolds, noting:

Looking for a bit more guidance? Click here to follow our hiLo game walk-through...

Tips:

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?

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