我正在尝试编写一个需要用户输入的小游戏。
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."
答案 0 :(得分:0)
你的意思是if "press" in x.lower() or "button" in x.lower()
?
>>> "press" in "some sentence with the word press in it"
True
>>> words = ["press","button","otherword"]
>>> any(w in "aaa press bbb" for w in words)
True
>>> any(w in "aaa button bbb" for w in words)
True
>>> any(w in "aaa bbb" for w in words)
False