简体   繁体   中英

Print a list from my class (python)

I have a class with information about customers. One of all the data elements is self.history were I save the history for every customer in a list. Every customer is also in a dictionary outside the Class were I have saved the name as key, and other information as values. My question is how do I print this list for every customer.

i have tried:

class Customer:
    def __init__(self, name, car, reg):
        self.name = name
        self.car = car
        self.reg = reg
        self.history = []


def add_new_customer:
    account = dict()
    name = input("Name: ")
    car = input("Car: ")
    reg = input("Regnummber : ")
    account[name] = Customer(name, car, reg)


def print_all_accounts():
    for keys in account:
        print(keys.history)

the code is bigger and I do a lot of things like name.history.append() and so on (500 lines). But this is the thing that doesn't work.

I get the error:

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

You're just looping over the dictionary keys.

for keys in account:
    print(account[keys].history)

or even better:

for key, value in account.items():
    print(value.history)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM