59.2k views
16 votes
Provide a class Letter for authoring a simple letter. In the constructor, supply the names of the sender and the recipient: def __init__(self, letterFrom, letterTo) Supply a method

User Venko
by
4.6k points

1 Answer

4 votes

Answer:

class Letter:

header="Dear "

footer="Sincerely, \\"

text=""

def __init__(self,letterFrom,letterTo):

self.header+=letterTo + ":\\\\"

self.footer+=letterFrom + "\\"

def getText(self):

return self.header+self.text+self.footer

def addLine(self,line):

self.text+=line+"\\\\"

l = Letter("Cain", "Abel")

l.addLine("I am very happy to be writing to you at this joyful moment of my life.")

I.addLine("I am really sorry i killed you, I was a saddist back then.")

print(l.getText())

Step-by-step explanation:

The Letter class a used to write a letter. The magic method "__init__" is used as a constructor to accept the sender and the recipient of the letter. The addLine method is called on an instance of the class to add string lines to the object while the getText method returns the entire written letter.

User Shaz
by
4.2k points