如果Input Len>20,则生成不同消息的子类方法

2024-06-16 12:46:55 发布

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

我正在尝试创建一个聊天机器人,可以与人类进行简单的对话。聊天机器人需要一个子类BoredChatbot,该子类将聊天机器人作为超类继承,但如果用户的输入长度超过20个字符,则会生成以下消息:

“zzz... Oh excuse me, I dozed off reading your essay.”

到目前为止,我已经:

^{pr2}$

这不起作用-它打印:

"It is very interesting that you say: + human_message" 

不管长度。在

我还试图切换if语句的顺序,使其出现在方法之外。在

class BoredChatbot(Chatbot):

def bored(self):
    """ Returns the Chatbot's response to length > 20 characters"""
    return "zzz... Oh excuse me, I dozed off reading your essay."  

sally = BoredChatbot("Sally")
human_message = input(sally.greeting())

if len(human_message) > 20:
    print(sally.bored(human_message))
else: 
    print(sally.response(human_message))

但这给了我一个错误信息:

AttributeError: 'Chatbot' object has no attribute 'bored' on line 31

为什么它在BoredChatbot内的方法注册时不觉得无聊?谢谢你帮我把这件事弄清楚-我觉得很接近了。在


Tags: message机器人子类sallymeohchatbothuman
1条回答
网友
1楼 · 发布于 2024-06-16 12:46:55

那么,让我们看看这里。在

class BoredChatbot(Chatbot):

    def bored(self):
    """ Returns the Chatbot's response to length > 20 characters"""
        if len(prompt_from_human) > 20:
            return "zzz... Oh excuse me, I dozed off reading your essay."
        else: 
            return(response)

什么是prompt_from_human?你怎么从bored(self)得到这个?另外,return(response)将抛出一些错误,因为1)self.response()是实际函数,但是2)response也没有定义。在


所以,解决这些问题,我认为您根本不需要bored函数。您应该重写response函数并返回超级函数,以保持对象函数的一致性。在

^{pr2}$

当然是sally = BoredChatBot("Sally")

相关问题 更多 >