Reputation: 1
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:
append
that takes one string argument and appends it to the message body as a new line__str__
that makes the message into one long string like this: "From: Harry Morgan\nTo: Rudolf Reindeer\n\nCome to the North Pole." 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
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
Reputation: 4644
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()
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)
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
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
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