【发布时间】: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