Subclass Method That Generates Different Message If Input Len > 20

1.1k views Asked by At

I'm trying to create a chatbot that can engage in simple conversation with a human. The chatbot needs a subclass, BoredChatbot, that inherits the Chatbot as a superclass, but generates the following message if the user's input is greater than 20 characters long:

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

So far I have:

class Chatbot:
    """ An object that can engage in rudimentary conversation with a human. """

    def __init__(self, name):
        self.name = name

    def greeting(self):
        """ Returns the Chatbot's way of introducing itself. """
        return "Hello, my name is " + self.name

    def response(self, prompt_from_human):
        """ Returns the Chatbot's response to something the human said. """
        return "It is very interesting that you say: '" + prompt_from_human + "'"


# define a class called BoredChatbot

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)

sally = Chatbot("Sally")
human_message = input(sally.greeting())
print(sally.response(human_message))    

This isn't working - it prints:

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

regardless of the length.

I also tried to switch the order of the if statement so that it appears outside of the method.

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))

But this gave me an error message:

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

Why isn't it registering bored from the method within BoredChatbot? Thanks for helping me clear this up - I feel like it's really close.

1

There are 1 answers

2
OneCricketeer On BEST ANSWER

So, let's look here.

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)

What is prompt_from_human? How do you get that from bored(self)? Also return(response) is going to throw some error because 1) self.response() is the actual function, but 2) response is not defined either.


So, fixing those problem, I don't really think you need a bored function at all. You should instead override the response function and return the super function in order to keep your object functions consistent.

class BoredChatbot(Chatbot):

    def response(self, prompt_from_human):
    """ 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 super(BoredChatbot, self).response(prompt_from_human)

Then sally = BoredChatBot("Sally"), of course