属性错误:"str"对象没有属性"公司"



我想不通。我想我对如何调用一个对象是错的。对象显然不被称为公司名称。

注册客户后,选择2进入子菜单显示客户列表,然后写下客户名称以显示有关特定客户的更多信息

# 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

请帮助D:

更多详细信息更多详细信息详细信息详细信息更多详细信息更多细节更多细节更多详情更多详情更多细节更多详细信息更多细节

这里有一个快速解决方案:

正如有人在评论中提到的那样,您正试图将字符串传递给期望特定对象的函数。您可以使用customerList来存储所有创建的对象,而不仅仅是company属性:

customerList.append(newCustomer)

对于显示现有客户修复,请使用以下片段:

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

代码:

# 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

最新更新