Xalarak Xalarak - 27 days ago 11
Python Question

AttributeError: 'str' object has no attribute 'company'

i cannot figure this out. i guess i'm wrong about how to call a object.
and objects are not called the company name, apperently.

after registered a customer, choose 2 to go to submenu to show customer list,
then write customer name to show more information about the specific customer

# APP REVOLUTIO SOLUTIONS - OverView ARS v1.0



class OVARS:

def __init__(self, company, kNr, adr, tlf, sp, app, appV, date, afd, ini):

self.company = company
self.kNr = kNr
self.adr = adr
self.tlf = tlf
self.sp = sp
self.app = app
self.appV = appV
self.date = date
self.afd = afd
self.ini = ini


class udskriv(OVARS):

def __init__(self):
super(OVARS, self).__init__()


def udskriv_kunde(self, cuna):
print(cuna.company)
print(cuna.kNr)
print(cuna.adr)
print(cuna.tlf)
print(cuna.sp)
print(cuna.app)
print(cuna.appV)
print(cuna.date)
print(cuna.afd)
print(cuna.ini)



######################## PROGRAM START ########################

udskrivKALD = udskriv()

print('OverView ARS v1.0')


customerList = []
i = 0

while True:

try:
menuChoice = int(input('''
1. apply new customer
2. show existing customers
3. exit
\n'''))
except ValueError:
print('choice not accepted.\n')
continue

print()

if menuChoice == 1:

addCompanyName = input('enter company name: ')
addKundeNr = input('enter customer ID.: ')
addAdresse = input('enter address: ')
addTelefon = input('enter telephone nr.: ')
addSP = input('enter Servicepackage: ')
addApp = input('enter app: ')
addAppV = input('enter App version: ')
addDate = input('enter Date: ')
addAfd = input('enter department: ')
addIni = input('enter Inititials: ')

customerList.append(addCompanyName)

print('\ncustomer registered: ' + str(addCompanyName) + '\n')

newCustomer = addCompanyName

newCustomer = OVARS(addCompanyName, addKundeNr, addAdresse, addTelefon, addSP, addApp, addAppV, addDate, addAfd, addIni)

continue


elif menuChoice == 2:

i = 0
j = 0
k = 1
for i in customerList:
print(str(k) + ': ' + customerList[j])
j += 1
k += 1


subMenu2Choice = input('''
choose customer to show customer information,
or choose 0 to go back to first menu
\n''')


if subMenu2Choice == 0:
continue

elif subMenu2Choice in customerList:
udskrivKALD.udskriv_kunde(subMenu2Choice)

else:
print('wrong choice')
continue


elif menuChoice == 3: # Exiting Program
break


else:
print('choice not accepted.\n')
continue

#udskrivKALD.udskriv_kunde(newCustomer)


print()

# END OF PROGRAM


PLEASE HELP D:

MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS MORE DETAILS

Answer Source

Here's a quick fix:

As someone already mentioned in the comment you are trying to pass in a string to a function that expects a particular object. You can use customerList to store all the created objects and not just the company attribute:

customerList.append(newCustomer)

For the show existing customers fix, use this snippet:

elif any(i.company==subMenu2Choice for i in customerList):
            for i in customerList:
                if i.company==subMenu2Choice:udskrivKALD.udskriv_kunde(i) 

Code:

# APP REVOLUTIO SOLUTIONS - OverView ARS v1.0
class OVARS:
    def __init__(self, company, kNr, adr, tlf, sp, app, appV, date, afd, ini):
        self.company = company
        self.kNr = kNr
        self.adr = adr
        self.tlf = tlf
        self.sp = sp
        self.app = app
        self.appV = appV
        self.date = date
        self.afd = afd
        self.ini = ini

class udskriv(OVARS):
    def __init__(self):
        super(OVARS, self).__init__()

    def udskriv_kunde(self, cuna):
        print(cuna.company)
        print(cuna.kNr)
        print(cuna.adr)
        print(cuna.tlf)
        print(cuna.sp)
        print(cuna.app)
        print(cuna.appV)
        print(cuna.date)
        print(cuna.afd)
        print(cuna.ini)

######################## PROGRAM START ########################

udskrivKALD = udskriv()

print('OverView ARS v1.0')

customerList = []
i = 0

while True:

    try:
        menuChoice = int(input('''
1. apply new customer
2. show existing customers
3. exit
        \n'''))
    except ValueError:
        print('choice not accepted.\n')
        continue

    print()

    if menuChoice == 1:

        addCompanyName = input('enter company name: ')
        addKundeNr = input('enter customer ID.: ')
        addAdresse = input('enter address: ')
        addTelefon = input('enter telephone nr.: ')
        addSP = input('enter Servicepackage: ')
        addApp = input('enter app: ')
        addAppV = input('enter App version: ')
        addDate = input('enter Date: ')
        addAfd = input('enter department: ')
        addIni = input('enter Inititials: ')



        print('\ncustomer registered: ' + str(addCompanyName) + '\n')

        newCustomer = addCompanyName

        newCustomer = OVARS(addCompanyName, addKundeNr, addAdresse, addTelefon, addSP, addApp, addAppV, addDate, addAfd, addIni)

        customerList.append(newCustomer)

        continue


    elif menuChoice == 2:

        i = 0
        j = 0
        k = 1
        for i in customerList:
            print(str(k) + ': ' + customerList[j].company)
            j += 1
            k += 1


        subMenu2Choice = input('''
choose customer to show customer information,
or choose 0 to go back to first menu
    \n''')


        if subMenu2Choice == 0:
            continue

        elif any(i.company==subMenu2Choice for i in customerList):
            for i in customerList:
                if i.company==subMenu2Choice:udskrivKALD.udskriv_kunde(i)

        else:
            print('wrong choice')
            continue


    elif menuChoice == 3: # Exiting Program
        break


    else:
        print('choice not accepted.\n')
        continue

    #udskrivKALD.udskriv_kunde(newCustomer)


print()

# END OF PROGRAM