【发布时间】:2019-04-09 01:27:56
【问题描述】:
我正在用 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?")
【问题讨论】:
-
cave == ('Y') or ('y')评估为(cave == 'Y') or True,始终为True。 -
顺便说一句,如果我说'N'然后是'Torch',如果我不在山洞里,我为什么会死?
标签: python-3.x