28. Test 4 – Condition controlled iteration

Main Task

  1. Create a program that:
    • Randomly generates a number between 1 -100
    • Asks the user to guess the number until they get it right
    • Says whether the guess is too high or too low
    • Says when they have guessed correctly, and quits

Extension Task

  1. Modify the program so it records the number of guesses taken

Hints

  • It may help to sketch out on paper the order in which you need to perform parts of the program
  • A good way of doing this is pseudocode, an example of which is shown below
  • Write pretty much what you like, but each line should translate directly into a line in your program. You should keep indents to show the bits that loop
import the random library
randomly generate a number between 1-100
set attempts to 0 
set guess to 0
while the guess is not equal to the random number
   ask for a guess to be input
   add one to the number of attempts
   if guess is less than random number tell user
   if guess is more than random number tell user
print a well done message and display the guessed number
print the number of attempts