Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果不是两个选项,您将如何使用IF NOT语句_Python - Fatal编程技术网

Python 如果不是两个选项,您将如何使用IF NOT语句

Python 如果不是两个选项,您将如何使用IF NOT语句,python,Python,对不起,我知道我的解释不是很好,所以我会给你看代码 def game(): level1 = input("You are stuck in the woods, lost after a school trip, your phone is dead so you must make your way through the woods. Do you turn left or right? ").lower()

对不起,我知道我的解释不是很好,所以我会给你看代码

def game():
        level1 = input("You are stuck in the woods, lost after a school 
        trip, your phone is dead so you must make your way through the 
        woods. Do you turn left or right? ").lower()
        if level1 == "left":
            print("You turned left and a bear mauled you to death")
        elif level1 != "left" or level1 != "right":
            print("Please choose either left or right")
            game()
        elif level1 == "right":
            print("Level 2")

抱歉,如果没有正确或清晰地呈现。每当我输入“right”时,它就会出现“请选择left或right”

我会使用
if
elif
else

if level1 == "left":
    print("You turned left and a bear mauled you to death")
elif level1 == "right":
    print("Level 2")
elif level1 == "cheat": #We can use as many 'elif' as needed
    print("No Cheating...!")
else: #any other input #but only one 'else'
    print("Please choose either left or right")
    game()

我想我找到了解决办法。问题部分在于:

elif level1 != "left" or level1 != "right":
有些东西不等于“左”就是“右”,所以当你输入
right
时,它会选择第一个逻辑

您必须执行以下操作:

if level1 == "left":
# Your code

elif level1 == "right":
# code

else:
# code
  • 但是最简单的方法是放置
    而不是

希望对您有所帮助

您提供的代码有什么错误?请使用
而不是
。或者,将整个条件更改为
elif level1 not in[“left”,“right”]:
几种修复方法:一个示例:
如果level1==“left”:。。。。elif level1==“右”:。。。否则