Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

issue with setting concatenated string variables in __init__ using variables set within __init__ as part of the concatenated string

I am writing a class called "USR" that holds some information about a user who creates an account on my command line e-commerce store (its a college group project that I’m having to do by myself bc ppl are lazy)

the contents of USR are as follows:

import os
class USR:
    def __init__(self,N,a,g,Uname,Pwd,Addr,CC):
        Name = N
        age = a
        gender = g
        Username = Uname
        Password = Pwd
        Address = Addr
        CCinfo = CC
        UserCart = os.getcwd() + self.Username + "_UC.csv"
        OrderHistory = os.getcwd() + self.Username +"_OH.csv"

UserCart and OrderHistory are supposed to be strings that when a user is created, grab the current directory, + the Username of the USR instance, + "_UC.csv" or "_OH.csv" respectively.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

in my testing function. i am doing the following:

    print("testing USR init")
    Usr = UserClass.USR("Andrew", 69,"Other","idk",1234,"some house",1234567891012131)
    print(Usr.UserCart)
    print(Usr.OrderHistory)    

when the code is run in the command line (OS = win10) i get the following error

line 12, in __init__,
UserCart = os.getcwd() + self.Username + "_UC.csv"
AttributeError: 'USR' object has no attribute 'Username'                   

I’m assuming this is because of the fact that the Username attribute is set in the same function? i don’t really know what to do about this.

Any tips or solutions are welcome, thank you!

I’ve tried setting them to use self.Name aswell, i still get the same attribute error. I’m not sure what else to try to solve the issue at hand.

>Solution :

def __init__(self,N,a,g,Uname,Pwd,Addr,CC):
    Name = N
    age = a
    gender = g
    Username = Uname
    Password = Pwd
    Address = Addr
    CCinfo = CC

You need to put self. in front of these variable names. self.Name = N, etc.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading