【问题标题】:AttributeError: 'str' object has no attribute 'company'AttributeError:“str”对象没有属性“company”
【发布时间】:2018-02-05 13:56:47
【问题描述】:

我无法弄清楚这一点。我想我错了如何调用一个对象。 并且对象显然不称为公司名称。

注册客户后,选择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:

更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节ou萱更多了【此树了详细了些源系之前异图更多了图解更多设计介图更多了介图饰介详了介页更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节 更多细节

【问题讨论】:

  • udskriv_kunde 需要一个 OVARS 实例 (cuna),但您传递的是一个字符串:udskrivKALD.udskriv_kunde(subMenu2Choice) (subMenu2Choice = input(...)。
  • 我知道,我的意图是你看到一个客户列表,然后在输入中写一个客户名称,但是注册时永远不会使用客户名称创建对象

标签: python class oop object


【解决方案1】:

这里有一个快速修复:

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

customerList.append(newCustomer)

对于显示现有客户修复,使用这个 sn-p:

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

【讨论】:

  • 它应该可以工作。只需在 subMenuChoice 2 中输入 companyName 字符串。
  • 哦。等等,你想输入条目的id还是公司名称来获取信息,??我当前的代码需要字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 2021-10-04
  • 2019-12-02
  • 2021-09-25
  • 2014-03-04
  • 2013-09-22
相关资源
最近更新 更多