Python中有一種方法可以搜索輸入字符串中的短語,然後返回如果有,則爲1;否則爲0?在Python中搜索短語的字符串輸入
我希望它是這樣工作的:
def findphrase(var):
if re.compile(r'\b({0})\b'.format(var), flags=re.IGNORECASE).search is True:
return 1
else:
return 0
def howareyou():
print("So",name, "how are you today?")
howis = input("")
if findphrase('not well')(howis) is 1:
print("Oh, that's not good. I hope you feel better soon")
elif findphrase('well')(howis) is 1:
print("That's good.")
elif findphrase('not bad')(howis) is 1:
print("Better than bad, I suppose.")
elif findphrase('bad')(howis) is 1:
print("Oh, that's not good. I hope you feel better soon")
elif findphrase('not good')(howis) is 1:
print("That's a shame. I hope you feel better soon.")
elif findphrase('good')(howis) is 1:
print("That's good.")
else:
print("I dont know how to respond to that. Keep in mind I am a work in progress. At some point I may know how to respond.")
完全可能的。看看're'模塊文檔。 – cge
還是'in'運營商,它告訴你一個字符串是否包含在另一個。 –
@ Rozza_15如果您的查詢已解決,請不要忘記[接受下面的答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work):) –