Page 67 -
P. 67

new game








                          You needed to rewrite your game program so it keeps running until the user guesses the
                           correct answer. You needed to use all of the things you’ve learned in this chapter. You
                           needed to work out the conditions for each of the branches and loops that are required.
                           Hint: If you need to test that two things have different values, use the != operator.







                                                                Don't worry if your code doesn't look
                           Did you remember to set the guess to   it works in the same way when you run it.
                           some sensible default value to make sure
                                                                exactly like this. The important thing is that
                           the loop ran the first time?


                                          print(“Welcome!")

                                          guess = 0
                   We need to keep running
                   while the guess is wrong.  while guess != 5:
                                              g = input(“Guess the number: ")

                                              guess = int(g)
             All of this code is indented, which       if guess == 5:
              means it's all inside the loop body.
                                                  print(“You win!")

                                              else:

                                                  if guess > 5:

                                                      print(“Too high")
                                                  else:

                                                      print(“Too low")

                                          print(“Game over!")

                                                                       This part of the program is very
                                                                       similar to what you had before.



           32    Chapter 1
   62   63   64   65   66   67   68   69   70   71   72