D Young
D Young

Reputation: 1

How do I stop getting this append error in python?

We have a class, Message, which models an e-mail message. A message has a recipient, a sender, and a message text. The class supports the following methods:

The error message I keep getting is at the bottom, how do I fix this?

Here is my code:

class Message:
    def __init__(self,sender,recipient,message=[]):
        self.sender = sender
        self.recipient = recipient 
        self.message = message

    def append(self,message): 
        self.message=self.message.append(message)

    def __str__(self): 
        print("\nFROM: "+sender+"\n"+"TO: "+recipient+"\n\n"+self.message)
def main():
    message = []
    sender = input('Who is sending the message: ')
    recipient = input('Who would you like to send the message to: ')
    body = input('Enter body (blank line to quit): ')
    while body != '':
        message.append(body)
        body = input('Enter body (blank line to quit): ')
        if body == '':
            break
    print()
    print('FROM:', sender)
    print('TO:', recipient)
    print()
    print(*message,sep='\n') 
if __name__ == '__main__':
    main()  

An error occurred when appending a line to the message!

Upvotes: 1

Views: 201

Answers (2)

Saurabh Sangwan
Saurabh Sangwan

Reputation: 427

def append(self,message): 
    self.message=self.message.append(message)

This will end up setting self.message to None. As you are setting it to the return value of append function which is None. You should use this instead.

def append(self,message): 
    self.message.append(message)

Upvotes: 0

Toothpick Anemone
Toothpick Anemone

Reputation: 4644

  1. You never instantiated your Message class. What I mean, is that you never wrote anything such as message = Message(). As such, no errors occur. I recommend something like the following:

    def main():
        sender = input('Who is sending the message: ')
        recipient = input('Who would you like to send the message to: ')
    
        message = Message(sender, recipient)
    
        body = input('Enter body (blank line to quit): ')
        while body != '':
            message.append(body)
            body = input('Enter body (blank line to quit): ')
            if body == '':
                break
    
        print(str(message))
    if __name__ == '__main__':
        main()
    
  2. Inside of __str__ you are missing self. before sender and before recipient

    def __str__(self):
        print("\nFROM: "+ self.sender+"\n"+"TO: "+self.recipient+"\n\n"+self.message)
    
  3. Currently, your __str__ method returns None and prints the string. What if you want the actual string? stryng = str(message) won't work, because stryng will be None

    def __str__(self):
        return "\nFROM: "+ self.sender+"\n"+"TO: "+self.recipient+"\n\n"+self.message
    
  4. Using + on strings, such as "hello world" == "hello" + ' ' +" world" is very inefficient. Almost never do it. Use a string streams instead:

    import io
    
    def __str__(self):
        with io.StringIO as ss:
            print(
                "\nFROM: ", self.sender,
                "\nTO: ", self.recipient,
                "\n\n", self.message,
                file = ss
            )
            stryng = ss.getvalue()
        return stryng
    
  5. append modifies "in-place". It does not return a modified copy.

    def append(self,message):
        self.message=self.message.append(message)
    

    If you don't write a return statement, what gets returned? None

    def append(self,message):
        self.message=self.message.append(message)
        return None
    

    What is on the right-hand-side of the = operator?

    def append(self,message):
        self.message = None
        return None
    

Upvotes: 1

Related Questions