Python错误(在构建模式匹配chatbot中):列表对象不是capab

2024-04-25 03:55:58 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我在我的基于python的迷你聊天机器人中出错了。 在我运行CommandPrompt时,会出现如下错误消息: TypeError:“list”对象不可用

这是我的密码:

import re
import random

response = (
    ("hello",                ("Hi!", "Hello!", "Greetings!", "Howdy!")),
    ("hi",                   ("Hi!", "Hello!", "Greetings!", "Howdy!")),
    )

pronouns = {
    "i'm": "you're", 
    "i": "you"
    }

random.seed()
print("I am psychiatrist bot. I can make you feel better. Tell me how you're feeling!")

while True:
    input = re.split("[\.!?]",input("> ").lower().rstrip(".!?")) # <-- Error
    full_reply=' '

    for sentence in input:
        sentence=sentence.lstrip()
        for pattern in responses:
            wildcards = []
            if re.match(pattern[0], sentence):
                wildcards = filter(bool, re.split(pattern[0], sentence))
                # replace pronouns
                wildcards = [' '.join(pronouns.get(word, word) for word in wildcard.split()) for wildcard in wildcards]

                response = random.choice(pattern[1])
                response = response.format(*wildcards)
                full_reply+=response+' '

                break

    print(full_reply)

Tags: inreyouforinputresponserandomreply