简体   繁体   English

为什么 if 语句有效,而 else 语句无效?

[英]Why do the if statements work, but NOT the else statements?

All if statements work, but the else statements in the first two if/else blocks return the following error?所有 if 语句都有效,但前两个 if/else 块中的 else 语句返回以下错误? 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

The issue is that the variable swim_or_wait is only defined in the first if block where the user chooses to go left.问题是变量swim_or_wait仅在用户选择 go 离开的第一个if块中定义。 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.如果用户选择 go 正确,则永远不会定义变量swim_or_wait ,并且第二个if语句的else块中的代码会抛出错误,因为它试图引用不存在的变量。 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.要解决此问题,您需要将swim_or_wait的定义移到第一个if块之外,以便不管用户的选择如何定义它。 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".此外,您应该在第一个if语句中添加一个else块,该语句定义swim_or_wait的默认值,以防用户输入“left”或“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.这样,如果用户选择右,变量swim_or_wait被定义并分配一个空字符串,如果用户选择左,变量被定义并分配用户输入的值。

swim_or_wait is only ever defined in the first if statement. swim_or_wait仅在第一个 if 语句中定义。 If that first if statement doesn't trigger, then this variable does not exist.如果第一个 if 语句没有触发,那么这个变量不存在。 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).你本质上有一个逻辑错误,因为你真的需要嵌套第二个 if 语句(呃,有更好更优雅的方法来做到这一点,但这对于听起来像的家庭作业项目就足够了)。

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.")


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM