I coded this in python 3 and it is showing an attribute error.code is as following:
import datetime
class MessageUser():
User_Details = []
Messages = []
base_message = """Hi {name}!
Thank you for the purchase on {date}.
We hope you are exicted about using it. Just as a
reminder the purcase total was ${total}.
Have a great time!
from Pritom_Mazhi
"""
def add_user(self, name, amount, email=None):
name = name[0].upper() + name[1:].lower() #Capitalizing the first letter of all names - formatted name
amount = "%.2f" %(amount) #formatted amount
detail = {
"name" : name,
"amount" : amount,
}
today = datetime.date.today()
date_text = '{tday.day}/{tday.month}/{tday.year}'.format(tday=today) #formatted date
detail["date"] = date_text
if email is not None:
detail["email"] = email
self.User_Details.append(detail)
def get_details(self):
return self.User_Details
def make_message(self):
if len(self.User_Details) > 0:
for detail in self.get_details(): #for detail in self.User_Details
name = detail["name"]
amount = detail["amount"]
date = detail["date"]
email = detail["email"]
message = self.base_message
formatted_message = message.format(
name = name,
total = amount,
date = date,
)
self.Messages.append(formatted_message)
return self.Messages
else:
return []
obj = MessageUser()
obj.add_user("Pritom", 123.32, email='[email protected]')
obj.add_user("jon Snow", 94.23)
obj.add_user("Sean", 93.23)
obj.add_user("Emilee", 193.23)
obj.add_user("Marie", 13.23)
obj.get_details()
obj.make_message()
when i run it i get this error:
File "Class_StringFormat.py", line 57, in <module>
obj.get_details()
AttributeError: 'MessageUser' object has no attribute 'get_details'
i simply can't find what wrong i did there and so can't manage to fix it.
Lev Levitsky
66.4k23 gold badges155 silver badges184 bronze badges
asked Dec 4, 2017 at 13:45
Samiul Salehin
111 gold badge3 silver badges8 bronze badges
-
You might want to rephrase the title of your question. It should be an indication of what you are asking.Manfred Radlwimmer– Manfred Radlwimmer2017年12月04日 13:47:10 +00:00Commented Dec 4, 2017 at 13:47
-
1Btw: The error could be caused by the indent.Manfred Radlwimmer– Manfred Radlwimmer2017年12月04日 13:48:05 +00:00Commented Dec 4, 2017 at 13:48
1 Answer 1
If your indentation is reproduced correctly in the question, get_details is defined inside add_user and is not visible from outside.
You should unindent the definition of get_details and make_message to be on the same level as add_user:
def add_user(self, name, amount, email=None):
# method body...
def get_details(self):
return self.User_Details
def make_message(self):
# method body
answered Dec 4, 2017 at 13:48
Lev Levitsky
66.4k23 gold badges155 silver badges184 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Samiul Salehin
ok,thank you very much for the solution.after I make the indentations correctly- if i do print(obj.make_message()) in the last line ,it only shows the message for "pritom" only and not for the"jon snow,sean,emilee,marie" etc. how can i fix that? i want the code work in a way that it would print all messages for all after it executes print(obj.make_message()) this line only once.
Lev Levitsky
@SamiulSalehin the problem is in make_message, where you have a return statement inside the for loop. It returns on the first iteration, and the rest of the loop is not executed. You need to return after the loop is finished.
Lev Levitsky
@SamiulSalehin great! Consider marking the answer as accepted to show that your problem is resolved.
Samiul Salehin
i'm new to ques and answering in stackoverflow.can you kindly tell me how to mark your answer as accepted?
Lev Levitsky
@SamiulSalehin you should see a tick to the left from the answer, which you can click on to accept it
lang-py