使用Python银行系统以相同的名称查找多个帐户

时间:2019-01-14 16:48:41

标签: python-3.x function for-loop error-handling syntax-error

在我的银行系统中,我有一组客户帐户,但是以亚当·史密斯(Adam Smith)的名字命名,他有两个帐户:

def load_bank_data(self):

    # the customers in the bank system
    account_no = 1234
    customer_1 = CustomerAccount("Adam", "Smith", 14, "Wilcot Street", "Bath", "B5 5RT", account_no, "Current", 2500.00)
    self.accounts_list.append(customer_1)

    account_no += 5678
    customer_2 = CustomerAccount("David", "White", 60, "Holburn Viaduct", "London", "EC1A 2FD", account_no, "Savings", 3200.00)
    self.accounts_list.append(customer_2)

    account_no += 3456
    customer_3 = CustomerAccount("Alice", "Churchil", 55, "Cardigan Street", "Birmingham", "B4 7BD", account_no, "Current", 18000.00)
    self.accounts_list.append(customer_3)

    account_no += 6789
    customer_4 = CustomerAccount("Ali", "Abdallah", 44, "Churchill Way West", "Basingstoke", "RG21 6YR", account_no, "Savings", 40.00)
    self.accounts_list.append(customer_4)

    account_no += 1987
    customer_5 = CustomerAccount("Adam", "Smith", 44, "Churchill Way West", "Basingstoke", "RG21 6YR", account_no, "Savings", 5000.00)
    self.accounts_list.append(customer_5)

我创建了一个函数,以便当找到许多具有相同名字和姓氏的客户帐户时,该功能应将所有这些银行帐户余额加在一起并打印出最终总计。 (输入是我输入客户以查找以下多个帐户的地方:

def sum_of_all_money(self):
    try:

        find_customer = input("Enter the surname of the customer to find total sum of money for: ")

        for find_customer in self.accounts_list:
            find_customer = find_customer.get_balance() + find_customer.get_balance()
        print(find_customer)

    except SyntaxError as e:
        print(e)

这只是在底部找到了一个5号客户的Adam Smith帐户,但没有检测到另一个1号客户的Adam Smith帐户,而只是将5号客户添加了两次,给了我1000.00的输出,这是不对的,我在做什么错了?

1 个答案:

答案 0 :(得分:0)

您的代码有一些缺陷,目前,它只会循环遍历您的列表,并始终用客户的当前余额* 2覆盖find_customer

您需要过滤输入的正确名称,像这样尝试:

try:

    find_customer = input("Enter the surname of the customer to find total sum of money for: ")

    find_customer_balance = 0

    for customer in self.accounts_list:
        if customer.get_surname() == find_customer:
           find_customer_balance += customer.get_balance()
    print(find_customer)
    print(find_customer_balance)

except SyntaxError as e:
    print(e)