Skip to content
Advertisement

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

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.

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement