【发布时间】:2014-12-08 22:35:28
【问题描述】:
我正在尝试编写一个需要用户输入的小游戏。
while True:
x = raw_input("\n> ")
if x in ["Susan", "Foreman"] and not grand_name:
print "The button glows! You have guessed the password! You can press the button now"
grand_name = True
elif x in ["Press", "press", "button"] and not grand_name:
print "You have to guess the password first!"
elif x in ["Press", "press", "button"] and grand_name:
print "The TARDIS materializes around the Doctor! He has been freed from a gaseous monster!"
end()
else:
print "Try Again."
我想让用户可以键入“按下按钮”或“我按下按钮”来满足所需的 elif 语句,而不是必须准确地键入列表的对象。如果单词本身在输入中的任何位置,我只想运行该语句。有没有一种方法可以让 Python 识别输入是否包含列表中的单词而无需准确输入单词?我希望这是有道理的。这是我问的第一个问题,所以我还在学习。谢谢。
我尝试使用如下示例中的 any(),但它返回错误“未定义全局名称 w”
当真时: x = raw_input("\n> ") opt1 = [“苏珊”,“工头”] opt2 = ["按下", "按下", "按钮"]
if any(z in x for z in opt1):
print "The button glows! You have guessed the password! You can press the button now"
grand_name = True
elif any(w in x for w in opt2) and not grand_name:
print "You have to guess the password first!"
elif any(w in x for x in opt2) and grand_name:
print "The TARDIS materializes around the Doctor! He has been freed from a gaseous monster!"
end()
else:
print "Try Again."
【问题讨论】:
-
我想你忘了问你的第一个问题。 :) 另见blogs.msdn.com/b/oldnewthing/archive/2007/03/15/1883515.aspx
-
哈哈哇抱歉。好的,这里是这样:有没有一种方法可以让 Python 识别输入是否包含列表中的单词而无需准确输入单词? 对不起,可怜我
标签: python if-statement plist raw-input