Home > Mobile >  Asking for input multiple times within a while loop
Asking for input multiple times within a while loop

Time:09-30

quit_game = "Goodbye, thank you for playing."

while True:
    tell_joke = print("Pete, Pete and Repeat went out on the lake in their boat. Pete and Pete fell out. Who is left in the boat? ")
    if input() == "REpeat":
        print(tell_joke)
        break
    elif input() == "Quit":
        print(quit_game)
    break;

I'm needing to loop back to the original "tell_joke" statement each time the user enters "REpeat" however it either prints the new defined input or reads off as none.

CodePudding user response:

In your while loop the break statement after the elif will be always executed, so your loop will run only one time. To avoid that, you can simply put the break inside the elif statement; speaking of breaks, if you want to repeat the cycle each time a "REpeat" is taken from input, then you want to delete the break inside the if statement. Besides, the input, in your code, is taken two times: a first time input() is called to check the if condition and another time to check the elif one. To avoid that, the first thing to do in your loop will be to save the user input into a variable.

Moreover, be careful, 'cause you're calling two times per cycle (when input "REpeat") the print() function and assigning its return value to tell_joke and, after that, you're calling again the print() function to print the tell_joke variable, which contains the result of print(), which explains the "casual" None output. To prevent multiple useless assignment, if you don't plan to change the value of tell_joke, you can declare and assign it before the whole loop, alongside the quit_game string. Remember, just to be more clear, that with only those two if statements, you are not handling any other input from the user than "REpeat" and "Quit", the loop will simply repeat itself.

If I correctly understand your desired output, it should look like this:

quit_game = "Goodbye, thank you for playing."
tell_joke = "Pete, Pete and Repeat went out on the lake in their boat. Pete and Pete fell out. Who is left in the boat?"
while True:
    my_input = input()
    if my_input == "REpeat":
        print(tell_joke)
    elif my_input == "Quit":
        print(quit_game)
        break

If you want to know more on the control flow tools in python, I redirect you to their documentation.

CodePudding user response:

You should be using continue if you want to loop back in the loop and break only if you want to exit. In your while loop you have a break and hence it never loop backs. Below is the code which I think you want:

quit_game = "Goodbye, thank you for playing."
tell_joke = "Pete, Pete and Repeat went out on the lake in their boat. Pete and Pete fell out. Who is left in the boat?"
while True:
    option = input('What to do? 1. Repeat 2. Quit: ')
    if option.lower() == "repeat":
        print(tell_joke)
        continue
    elif option.lower() == "quit":
        print(quit_game)
        break
    else:
        print ("Invalid option provided..provide the right one")

Output:

What to do? 1. Repeat 2. Quit: bh
Invalid option provided..provide the right one
What to do? 1. Repeat 2. Quit: Repeat
Pete, Pete and Repeat went out on the lake in their boat. Pete and Pete fell out. Who is left in the boat?
What to do? 1. Repeat 2. Quit: Quit
Goodbye, thank you for playing.
  • Related