@extends('template') @section('title') Lab 5: Part 2 - While Loops @stop @section('head') @stop @section('content') # 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** ```xml >>> 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)** ```xml >>> 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()`: ```py 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). ```py # 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: ```xml 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... ``` @include('/labs/lab05/_toc') @stop