为什么这个字符串比较不起作用? (差异库)
Why is this string comparison not working? (difflib)
当用户说 "what's your name" 之类的内容时,此代码应该打印 "I am unit Alpha 07",但由于某种原因,if 语句永远不会 returns 为真。请帮忙!
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("I am unit Alpha 07")
break
continue
input()
Here's a screenshot
抱歉,如果这真的很愚蠢。
因为 temp
是 list
并且您想检查 if
该列表的第一个 element
是 What is your name?
那么您不能只将所有操作都作为 string
进行,就像 "['What is your name?']"
一样,您需要检查第一个元素(索引 0
),然后进行比较:
if temp[0] == "What is your name?":
...
这会奏效。祝你好运!
当用户说 "what's your name" 之类的内容时,此代码应该打印 "I am unit Alpha 07",但由于某种原因,if 语句永远不会 returns 为真。请帮忙!
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("I am unit Alpha 07")
break
continue
input()
Here's a screenshot
抱歉,如果这真的很愚蠢。
因为 temp
是 list
并且您想检查 if
该列表的第一个 element
是 What is your name?
那么您不能只将所有操作都作为 string
进行,就像 "['What is your name?']"
一样,您需要检查第一个元素(索引 0
),然后进行比较:
if temp[0] == "What is your name?":
...
这会奏效。祝你好运!