[英]How do i make an if statement if my answer includes something
i am a fairly average coder and i would love to know that if my player says something rand but it includes keywords it would still do a if statement? 我是一个相当普通的编码器,我很想知道,如果我的播放器说的是兰德,但它包含关键字,它仍然会执行if语句?
For example. 例如。 Answer = input("Take lantern or leave") if answer ==(Includes like "Pickup" instead of take i want this to still run or if they say pickup the lantern etc. 答案=输入(“带灯笼或离开灯笼”),如果答案==(包括类似“提货”的提示,而不是我希望它仍然运行,或者他们说拿灯笼等)。
I hope you can help me, if any additional info needed just ask 希望您能为我提供帮助,是否需要其他信息
print ("You can barley see anything around you but can hear loud noises coming from within the cave") print ("") answer = input (" There is a blue door onfront of you, beside you, beside you is a lantern of which is rusty but working ") 打印(“大麦可以看到你周围的任何东西,但可以听到洞穴内传来的巨响”)打印(“”)答案=输入(“你前面有一个蓝色的门,在你的旁边,旁边是一个灯笼生锈但可以正常工作“)
if answer == "take lantern" or answer == "Take lantern":
I want it so they don't have to precisely put "Take lantern or something like that, i want keywords like "Pickup" or "Take" 我想要这样,所以他们不必精确地输入“拿灯笼或类似的东西,我想要像“皮卡”或“拿”这样的关键字
You could either check if your answer
is in a list of expected answers: 您可以检查您的answer
是否在预期答案列表中:
answer = input("...")
if answer in ["take", "pickup"]:
...
Or you could check your answer
with Regular Expressions: 或者,您可以使用正则表达式检查answer
:
import re
...
if re.search("(take|pickup)", answer):
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.