In python my if statement in my while true loop keeps outputting the same "invalid input" text until the program eventually crashes for my text game

76 views Asked by At
print(" You can see a faint outline of a coast in the distance... you can either: 1. paddle to the island on the raft ; 2. swim to the island")
    option2 = input("> ")
    while True:
        if (option2 == "1"):
            print("The raft fully sinks... you are left in the freezing cold water. You spent all your     energy trying to keep the raft afloat. You have made your demise inevitable")
            break
        elif (option2 == "2"):
            print("You jump headfirst into the cold water...  not really a good idea, but you seem to have insane swimming skills. Are you Michael Phelps? Anyways you make it to the shore seconds before fainting")
        else:
            print(" Invalid input: Please select one of the options above") <---- It keeps printing this
            continue

This is the code that I used. I'm new to Python and I really don't know how while loops function

I 've tried finding syntax issues but I can't spot any

2

There are 2 answers

1
sashkins On

Now it gets looping infinitely if you enter an option that is not "1". You will get into the else statement, which contains the 'continue' keyword.

'continue' means to start the next iteration. And as far as your option is captured only once (before the loop), its never gets changed, so you will be falling into the else block always.

In order to make it work, you need to move your "option2 = input("> ")" inside of the loop and add a break for the second elif:

print("You can see a faint outline of a coast in the distance... you can either:\n1. paddle to the island on the raft;\n2. swim to the island;")

while True:
    option2 = input("> ")
    if option2 == "1":
        print("The raft fully sinks... you are left in the freezing cold water. You spent all your energy trying to keep the raft afloat. You have made your demise inevitable")
        break
    elif option2 == "2":
        print("You jump headfirst into the cold water... not really a good idea, but you seem to have insane swimming skills. Are you Michael Phelps? Anyways, you make it to the shore seconds before fainting")
        break
    else:
        print("Invalid input: Please select one of the options above")

You can read more about how the while loops work here

7
Sumon2j On

You are getting this error because the while loop doesn't consist exit condition for the elif and else condition of the while loop. You will not face this issue for option 1 as it has a break condition which will help to exit the program from the while loop. Moreover, as you are using an infinite while loop I'm assuming you need input after each print to keep the iteration alive. So, You need to add an exit condition in the if-else scope and also an input for new data after the if-else scope. Below is the correct code added:

print(" You can see a faint outline of a coast in the distance... you can either: 1. paddle to the island on the raft ; 2. swim to the island; 3.Exit") 
option2 = input("> ") 
while True: 
    if (option2 == "1"): 
        print("The raft fully sinks... you are left in the freezing cold water. You spent all your energy trying to keep the raft afloat. You have made your demise inevitable")
    elif (option2 == "2"): 
        print("You jump headfirst into the cold water... not really a good idea, but you seem to have insane swimming skills. Are you Michael Phelps? Anyways you make it to the shore seconds before fainting")
    elif (option2 == "3"):
        break
    else: 
        print(" Invalid input: Please select one of the options above")
    option2 = input(">")

The above code will help you to take inputs during infinite iteration. Also, I have added one condition that if you input "3" , the loop will be terminated.