Hints for Hi/Lo game
-
Inside
hiLo()
generate a random number between 1 and 50, inclusive, and assign it to a variable. Test this and make sure it works before proceeding. -
Write a while loop. When will your loop stop executing? Use that to write your boolean expression.
- Experiment with the provided
getUsersGuess()
to make sure you understand how it works. The function returns the integer that the user guesses (and assumes that the user types in a positive number).
In [17]: getUsersGuess()
Take a guess: 5
Out[17]: 5
In [18]: getUsersGuess()
Take a guess: 10
Out[17]: 10
In hiLo()
, inside your while loop, assign the user's guess to a variable.
- Write your
if
statement to cover these cases:
- If the guess is higher than the secret number, print
Too high
- If the guess is lower than the secret number, print
Too low
- If the guess is the same as the secret number, print
Wahoo
(or your fave celebratory expression)
-
Run your code to see if the condition works. Run it a bunch of times to make sure it works in different cases. Don't worry about the game ending correctly (yet).
-
Now think about getting the loop to stop when you want it to. Hint: use
break
. -
Add the print statement at the appropriate place when the user has run out of guesses and reveal the secret number.
- If the user guesses correctly, make sure you print the number of guesses after your wahoo message.
← Return to Hi/Lo instructions