Page 68 -
P. 68

starting to code





                    Ready Bake
                    Code

                         If you add these two lines of code to the top of your program:
                            from random import randint
                            secret = randint(1, 10)
                         The secret variable will be set to a random number between 1 and 10. You were to
                         modify your program from the facing page so that instead of the answer always being
                         5, it will instead use a random number from 1 to 10 as the answer.




                   Here are the two      from random import randint
                   lines that create the   secret = randint(1, 10)
                   random number.

                                         print(“Welcome!")
                                         guess = 0

                                         while guess != secret:

            Now, instead of checking if the       g = input(“Guess the number: ")
            answer's 5, we check it against
            the random number, which is held       guess = int(g)
            in the “secret" variable.        if guess == secret:

                                                 print(“You win!")

                                             else:

                                                 if guess > secret:
                                                     print(“Too high")

                                                 else:

                                                     print(“Too low")
                                         print(“Game over!")








                                                                                        you are here 4    33
   63   64   65   66   67   68   69   70   71   72   73