所以基本上我一直在用python创建一个小的叙述选择游戏,它有很多选择,所以它有很多其他选择,但对为什么它们不起作用感到困惑 我试图移动东西,但不占上风
print("On the large worktop you see a pot filled with a stew it seems rather fresh")
answer2=input("do you eat it?")
if answer2=="yes" or "Yes" or " yes":
print("The salty taste of the stew makes you splutter but fills your stomach,You gain 3 hp")
else:
print("Out of caution you leave the stew on the work top")
answer3=input("As you leave the kitchen the tunnel splits into two do you go right or left?")
if answer3=="left" or "Left":
print("you head down the left route you hear a slow click spikes rise from the ground and impale your foot you loose 3 hp and slowly limp back to the to the start of the passage and make your way down the right side")
else:
print("you walk down to the end of the passage way")
答案 0 :(得分:2)
您的if语句有问题,因为它们不完整。您要考虑的每个或都必须是完整的逻辑表达式。
if answer2 == "yes" or answer2 == "Yes" or answer2 == " yes":
if answer3 == "left" or answer3 == "Left":
答案 1 :(得分:1)
给出了食谱,但我只想阐述一点:
if x == "yes" or " yes "
将始终返回True
,因为or " yes "
基本上是在询问python " yes "
是否为None
。而且它不是None
,因为它是yes
。
如上所述,您希望将其更改为以下内容:
if answer == "Yes" or answer == " Yes ":
...
我实际上建议您进一步介绍一下:
if "yes" in answer.lower():
...
这样,您可以检查答案是否是肯定的(显然,这样做有缺点,以确保您在其他问题中不使用yes
),但可以帮助您的代码更易于维护。
答案 2 :(得分:0)
在python中使用关系运算符时,请确保在与and
和or
一起使用时,在运算符的左侧和右侧都存在操作数。
执行类似if answer2=="yes" or "Yes" or " yes":
的操作,将始终执行if
块中的语句,因为这将始终为true
。因此,当用户输入answer2
的值而不是“是”或“是”时,if语句中存在的语句将不会执行。
print("On the large worktop you see a pot filled with a stew it seems rather fresh")
answer2=input("do you eat it?")
if answer2 == "yes" or answer2 == "Yes":
print("The salty taste of the stew makes you splutter but fills your stomach,You gain 3 hp")
else:
print("Out of caution you leave the stew on the work top")
answer3=input("As you leave the kitchen the tunnel splits into two do you go right or left?")
if answer3 == "left" or answer3 == "Left":
print("you head down the left route you hear a slow click spikes rise from the ground and impale your foot you loose 3 hp and slowly limp back to the to the start of the passage and make your way down the right side")
else:
print("you walk down to the end of the passage way")