当用户问“你叫什么名字”这样的问题时,这段代码应该打印“Adam”。相反,if 语句永远不会返回 true。请帮忙!
import difflib
while True:
talk = input("Please say something > ")
temp = (difflib.get_close_matches(talk, ['What is your name?', 'Hello', 'peach', 'puppy'],1,0.2))
print(temp)
if temp == "['What is your name?']":
print("Adam")
break
continue
input()
如果这是一个愚蠢的问题,请提前道歉。
由于
temp
是 list
并且您想检查 if
该列表中的第一个 element
是 What is your name?
那么您不能将其全部作为 string
来完成,就像这样 "['What is your name?']"
正如您所做的那样,您需要检查第一个元素(索引 0
),然后进行比较:
if temp[0] == "What is your name?":
...
这会起作用。祝你好运!