我正在用Python 3教我的学生一些简单的代码,并且我们使用if / elif / else语句以及用户输入来编写简单的“选择自己的冒险”故事。我第一次编写此代码,它按预期运行。现在,无论输入了什么,它始终提供第一个选项。谁能解释为什么?谢谢!感谢大家!我的问题已解决。
cave=input('Would you like to enter the cave? Answer Y or N')
if cave == ('Y') or ('y'):
print('The cave is cold.')
elif cave == ('N') or ('n'):
print('Stay in the sunshine. Safe, but boring.')
else:
print('That was not one of the choices.')
tool= input ("You have two items in your bag: a torch and a sandwich.
Which would you like to use?")
if tool == ("torch"):
print ("The fire ignites the deadly gas that has built up in the
cave. You die.")
elif tool ==("sandwich"):
print ("Good idea. You'll need strength to explore the cave.")
else:
print ("Why do you insist on making things difficult?")
答案 0 :(得分:0)
此语句的计算是对还是错
if cave == ('Y') or ('y'):
应该是
if cave == ('Y') or cave == ('y'):
答案 1 :(得分:0)
在Python中,没有“多重方程式检查”。当您要创建它时,您应该使用类似以下的内容:
if a == b or a == c:
#some code here...
您还可以在中使用:
if a in (b, c):
#some code here...
或
if a in [b, c]:
#more code...
(这有点慢,所以在游戏中,我推荐第一个)