2017-04-20 49 views
1

我有一個關於客戶信息的課程。當我列出每個客戶的歷史記錄時,所有數據元素之一是self.history。每個客戶也都在班級以外的字典中,我已經將名稱保存爲密鑰,並將其他信息保存爲值。我的問題是如何爲每個客戶打印此列表。打印我的課程列表(python)

我曾嘗試:

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) 

的代碼是做大我做了很多事情像 name.history.append()等(500線)。但是這是行不通的。

我得到的錯誤:

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

回答

2

你只是遍歷的字典鍵。

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

甚至更​​好:

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