Python Forum
code that takes inputs for user name amounts etc and then sends custom message
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code that takes inputs for user name amounts etc and then sends custom message
#1
I was trying this cose and I am not sure why this is not working as I am new to Python hence I am not abel to figure this out. Just trying to create a class and definitons through which I can add user and then also send them a message. Please need help on this cose not sure why I am getting errors like ,
Error:
"SyntaxError: multiple statements found while compiling a single statement" "Traceback (most recent call last):   File "<pyshell#36>", line 1, in <module>     obj.add_user("justin", 123.32) AttributeError: 'MessageUser' object has no attribute 'add_user'" Traceback (most recent call last):   File "<pyshell#38>", line 1, in <module>     obj.get_details() AttributeError: 'MessageUser' object has no attribute 'get_details'
Here is the Code,
import datetime

class MessageUser():
    user_details=[]
    messages=[]
    base_message="""Hi {name}!
Thank you for the purchase on {date}.
We hope you are exited about using it. Just as a
reminder the purchase total was ${total}.
Have a great one!

Team CFE
"""

def add_user(self, name, amount):
    name=name[0].upper()+name[1:].lower()
    amount="%.2f" %(amount)
    detail={
        "name": name,
        "amount": amount,
    }
    today=datetime.date.today()
    date_text='{today.month}/{today.day}/{today.year}'.format(today=today)
    detail['date']=date_text
    self.user_details.append(detail)

def get_details(self):
    return self.user_details

def make_messages(self):
    if len(self.user_details)>0:
        for detail in self.get_details():
            name=detail["name"]
            amount=["amount"]
            date=detail[date]
            message=self.base_message
            new_msg=unf_mesage.format(
                name=name,
                amount=amount,
                date=date
            )
            self.messages.append(new_msg)
        return self.messages
    return[]

obj=MessageUser()
obj.add_user("justin",123.32)
obj.add_user("john",94.32)
obj.get_details()
Really need help....
Reply
#2
In the future, please wrap your code in the "code" tags.  I did it for you this time.

If your original code is as you posted, all your functions are incorrectly indented.  I presume you mean to have them all under the class MessageUser?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
as sparkz said, you have your code incorrectly indented. The fact that you have all the methods dedented it is not apart of the class, and hence that object does not have those methods at all. Be extra careful with indentation in python...especially when copying/pasting code that it comes out as expected. 
import datetime
 
class MessageUser():
    user_details=[]
    messages=[]
    base_message="""Hi {name}!
    Thank you for the purchase on {date}.
    We hope you are exited about using it. Just as a
    reminder the purchase total was ${total}.
    Have a great one!
     
    Team CFE
    """
     
    def add_user(self, name, amount):
        name=name[0].upper()+name[1:].lower()
        amount="%.2f" %(amount)
        detail={
            "name": name,
            "amount": amount,
        }
        today=datetime.date.today()
        date_text='{today.month}/{today.day}/{today.year}'.format(today=today)
        detail['date']=date_text
        self.user_details.append(detail)
     
    def get_details(self):
        return self.user_details
     
    def make_messages(self):
        if len(self.user_details)>0:
            for detail in self.get_details():
                name=detail["name"]
                amount=["amount"]
                date=detail[date]
                message=self.base_message
                new_msg=unf_mesage.format(
                    name=name,
                    amount=amount,
                    date=date
                )
                self.messages.append(new_msg)
            return self.messages
        return[]
 
obj=MessageUser()
obj.add_user("justin",123.32)
obj.add_user("john",94.32)
print(obj.get_details())
Recommended Tutorials:
Reply
#4
Also looks like you're missing comma's, if still an error after correcting indents
Reply
#5
(Feb-11-2017, 10:22 PM)Larz60+ Wrote: Also looks like you're missing comma's, if still an error after correcting indents

While other's are so overstocked in apostrophe's that they put them in place's where they aren't necessary :)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#6
(Feb-12-2017, 10:49 AM)Ofnuts Wrote: While other's are so overstocked in apostrophe's that they put them in place's where they aren't necessary :)

It makes up for various periods that have mysteriously gone missing. :P
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 512 Nov-23-2023, 02:53 PM
Last Post: rob101
  Code freez if more inputs kiko058 2 775 Mar-28-2023, 03:52 PM
Last Post: kiko058
  how to make bot that sends instagram auto password reset code kraixx 2 1,274 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Try,Except,Else to check that user has entered either y or n (Code block pasted) RandomNameGenerator 3 2,302 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 2,646 Jan-28-2021, 05:22 PM
Last Post: snippsat
  Help with User Interface design code ai_masti 0 1,898 Nov-19-2020, 05:58 PM
Last Post: ai_masti
  poplib - parsing message body, could somebody please help explain this code t4keheart 2 2,248 Oct-12-2020, 01:59 PM
Last Post: t4keheart
  server selects Runners from list and sends model alex80 2 2,012 Sep-07-2020, 09:08 AM
Last Post: alex80
  how to create my own custom logging message maiya 4 2,299 Jul-15-2020, 05:42 PM
Last Post: maiya
  Hi, I need help with defining user's input and applying it to code. jlmorenoc 2 2,235 Jun-24-2020, 02:10 PM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020