Home > Net >  Why do the if statements work, but NOT the else statements?
Why do the if statements work, but NOT the else statements?

Time:01-28

All if statements work, but the else statements in the first two if/else blocks return the following error? Can someone please tell me why this is?

Traceback (most recent call last):
  File "main.py", line 36, in <module>
    if swim_or_wait.lower() == "wait":
NameError: name 'swim_or_wait' is not defined

code

left_or_right = input("Choose which way you want to go. Left or right? ")

if left_or_right.lower() == "left":
  swim_or_wait = input("Do you want to swim or wait? ")
else:
    print("Wrong way. You fell into a hole. Game over.")

if swim_or_wait.lower() == "wait":
  which_door = input("Choose which door you want to go through? Red, blue, or yellow? ")
else:
  print("You've been attacked by a trout. Game Over.")

if which_door.lower() == "red":
  print("Burned by fire. Game Over.")
elif which_door.lower() == "blue":
  print("Eaten by beasts. Game Over.")
elif which_door.lower() == "yellow":
  print("Congratulations!!! The game is yours. You Win!")
else:
  print("Wrong door. Game Over.")`your text

CodePudding user response:

swim_or_wait is only ever defined in the first if statement. If that first if statement doesn't trigger, then this variable does not exist. You essentially have a logic error as really you need to have this second if statement nested (ugh, there are better more elegant ways of doing this but this will suffice for a homework project that this sounds like).

Forgive my formatting below on the indentations.

Par Example:

left_or_right = input("Choose which way you want to go. Left or right? ")

if left_or_right.lower() == "left":
    swim_or_wait = input("Do you want to swim or wait? ")
    if swim_or_wait.lower() == "wait":
        which_door = input(
            "Choose which door you want to go through? Red, blue, or yellow?"
        )
        ## You'd be better with a case switch here
        if which_door.lower() == "red":
            print("Burned by fire. Game Over.")
        elif which_door.lower() == "blue":
            print("Eaten by beasts. Game Over.")
        elif which_door.lower() == "yellow":
            print("Congratulations!!! The game is yours. You Win!")
        else:
            print("Wrong door. Game Over.")
    else:
        print("You've been attacked by a trout. Game Over.")
else:
    print("Wrong way. You fell into a hole. Game over.")


CodePudding user response:

The issue is that the variable swim_or_wait is only defined in the first if block where the user chooses to go left. If the user chooses to go right, the variable swim_or_wait is never defined and the code in the else block of the second if statement throws an error because it is trying to reference a variable that does not exist. To fix this, you need to move the definition of swim_or_wait outside the first if block so that it is defined regardless of the user's choice. Additionally, you should add an else block in the first if statement that defines a default value for swim_or_wait in case the user enters something other than "left" or "right". So, your code should look like this:

left_or_right = input("Choose which way you want to go. Left or right? ")
swim_or_wait = ""
if left_or_right.lower() == "left":
  swim_or_wait = input("Do you want to swim or wait? ")
else:
    print("Wrong way. You fell into a hole. Game over.")

if swim_or_wait.lower() == "wait":
  which_door = input("Choose which door you want to go through? Red, blue, or yellow? ")
else:
  print("You've been attacked by a trout. Game Over.")

if which_door.lower() == "red":
  print("Burned by fire. Game Over.")
elif which_door.lower() == "blue":
  print("Eaten by beasts. Game Over.")
elif which_door.lower() == "yellow":
  print("Congratulations!!! The game is yours. You Win!")
else:
  print("Wrong door. Game Over.")

This way, if the user chooses right, the variable swim_or_wait is defined and assigned with an empty string, and if the user chooses left, the variable is defined and assigned with the value of the user's input.

  • Related