【问题标题】:Finding multiple accounts under the same name for Python bank system为 Python 银行系统查找同名的多个帐户
【发布时间】:2019-01-14 16:48:41
【问题描述】:

在我的银行系统中,我有一组客户账户,但亚当·史密斯的一个名字有两个账户:

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)

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

【问题讨论】:

  • 您以两种不同的方式使用find_customer:作为输入,以及作为在您的帐户列表中迭代的对象。
  • 好的,我取出输入,它现在显示每个客户的所有余额,add 类是否适合将两个 Adam Smith 余额相加?跨度>
  • 您是否首先需要一个if 条件来检查该帐户是否属于史密斯?

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


【解决方案1】:

您的代码有一些缺陷,目前它只会遍历您的列表,并且总是用客户的当前余额 * 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)

【讨论】:

  • 哦,是的,就是这样,非常感谢。我只是在努力寻找如何让 Python 过滤客户名称并找到匹配的名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
  • 2014-12-06
  • 2010-12-14
  • 2019-04-14
相关资源
最近更新 更多