You must first complete Making a basic guessing game before viewing this Lesson

Looping the Guessing Game

Introduction

Now that we have the basic guessing game ready, the next step is to have the option to replay the game so we don’t have to manually run it every time!

Components Needed

  • Little Python Brain
  • A tablet/phone/laptop
Steps

1

The main loop

Simply drag a while True to the workspace and drag the whole for loop as well as the random integer generator inside it and the answer feedback after the for loop.

2

Adding a retry prompt

At the end of the while loop and after the main for loop, we will add another User_Input block to prompt the user if they would like to try again. We only want to take one of two inputs: “y” (yes) or “n” (no)

3

More conditional statements

Now, we will then make conditional statements to check for the input.

if tryAgain is "y":
    pass

Pass is used as a placeholder or a buffer for codes. In this case, when the program reads the pass block, it automatically disregards the next conditional statement, which is

elif tryAgain is "n":
     break

Break, in this case, will end the while True loop.

The Complete Code

Run the code. Have fun!

Back to: Intermediate > Guessing Game
© 2020 Little Python

CONTACT US

    Log in with your credentials

    or    

    Forgot your details?

    Create Account