【发布时间】:2015-07-26 00:36:29
【问题描述】:
我有一个简单的 if elif else 语句,它依赖于用户的输入。
options = ['Try to jump the gap', 'Go back to the entryway']
print "You can:"
for x in options:
print "\t:>%s" % x
choice = raw_input("What do you do?")
if 'try' in choice:
print "You try to jump the gap and fail."
print "You fall into the pool of acid while shrieking in pain."
print "You dissolve"
dead()
elif 'go' in choice:
change()
entrywaymain()
else:
unknown()
change()
poolroomclosed()
当用户只输入“try”或“go”时,该代码有效。 但是,如果用户输入完整的语句,无论是“尝试跳过间隙”还是“回到入口通道”,他们总是会得到 if 值并且会死。那么,我如何编码才能激活布尔值用户输入中只有该字符串中的单词。
【问题讨论】:
-
可能是
choice.lower() == 'try'或choice.lower().startswith('try')?
标签: python if-statement boolean