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==“右”:。。。否则代码>