gpt4 book ai didi

python-3.x - 使用Python银行系统以相同的名称查找多个帐户

转载 作者:行者123 更新时间:2023-12-03 08:46:01 25 4
gpt4 key购买 nike

在我的银行系统中,我有一组客户帐户,但以亚当·斯密为名,他有两个帐户:

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帐户,但并没有将另一个Adam Smith帐户作为客户1进行检测,而只是将5号客户添加了两次,给了我1000.00的输出,这是不对的,我做错了吗?

最佳答案

您的代码有一些缺陷,目前仅会遍历您的列表,并始终用客户的当前余额* 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-3.x - 使用Python银行系统以相同的名称查找多个帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54185798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com