Python电子邮件消息未生成输出
我必须设计一个类消息来模拟电子邮件消息。邮件包含收件人、发件人和邮件文本。设计类以支持以下方法:Python电子邮件消息未生成输出,python,Python,我必须设计一个类消息来模拟电子邮件消息。邮件包含收件人、发件人和邮件文本。设计类以支持以下方法: 将发送方和接收方作为参数的构造函数 append,它接受一个字符串参数并将其作为新行追加到消息体 str将信息转换为一个长字符串,如下所示:“发件人:哈里·摩根\n收件人:鲁道夫·驯鹿\n\n抵达北极。” 我的代码没有生成输出,我已尝试更改间距。我觉得这个密码好像是对的。我对编码相当陌生,所以可能会遗漏一些东西。感谢您的帮助 这是我的信息。课堂: class Message: def _
“发件人:哈里·摩根\n收件人:鲁道夫·驯鹿\n\n抵达北极。”
class Message:
def __init__(self, sender, recipient):
self.sender = sender
self.recipient = recipient
self.body = ""
def append(self, line):
self.body += line + "\n"
def toString(self):
messageString = "From: {}\nTo: {}\n{}\n".format(self.sender, self.recipient, self.body)
return messageString
您已经有了一个良好的开端,您已经创建了一个类,但是在您实际实例化对象之前不会发生任何事情 我对代码的结尾做了一些修改/注释,希望这能帮助您更好地理解它
class Message:
def __init__(self, sender, recipient):
self.sender = sender
self.recipient = recipient
self.body = ""
def append(self, line):
self.body += line + "\n"
def toString(self):
messageString = "From: {}\nTo: {}\n{}\n".format(self.sender, self.recipient,
self.body)
return messageString
sender = input('Who is sending this message?') #creating a string to pass to class
recipient = input('Who is receiving this message?') #same as above
body = input('What would you like to send?') #this is for your append method
email = Message(sender, recipient) #instantiating the class
email.append(body) #using your append method to add the body
print(email.toString()) #printing the results of your toString method(which I recommend renaming for clarity)
您的代码不产生输出是什么意思
toString
明确返回一个值。如何创建实例?如何调用toString方法?要补充Joshua之前的评论,您的意思是它不打印任何内容吗?在这种情况下,尝试使用print(messageString)
而不是return
。它将生成发送电子邮件的输出。例如:谁在发送信息:您想将信息发送给谁:输入body(要退出的空行):输入body(要退出的空行):输入body(要退出的空行):FROM:Alice to:Smith教授您明天上午9:30有空吗?我的数学作业需要帮助。