为什么这个字符串比较不起作用? (差异库)

问题描述 投票:0回答:1

当用户问“你叫什么名字”这样的问题时,这段代码应该打印“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()

如果这是一个愚蠢的问题,请提前道歉。

python-3.x if-statement difflib
1个回答
0
投票

由于

temp
list
并且您想检查
if
该列表中的第一个
element
What is your name?
那么您不能将其全部作为
string
来完成,就像这样
 "['What is your name?']"
正如您所做的那样,您需要检查第一个元素(索引
0
),然后进行比较:

if temp[0] == "What is your name?":
    ...

这会起作用。祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.