银行系统的账户使用字典来存储,但它只读取第一个账户

时间:2021-06-18 14:36:59

标签: python

我正在创建一个带有帐户的银行系统,但是如果我输入了 2 个或更多帐户,则第二个帐户无法读取并且无法仅登录第一个有效的帐户

accounts = []
names = [item.get('fullname') for item in accounts]
customers = int(input("Number of customers: "))
while customers != 0:
    fullname = str(input("Input Fullname: "))
    while fullname in names:
        print('Name already exist')
        fullname = str(input("Input Fullname: "))
    else:
        pass
    pins = str(input("Please input pin of your choice: "))
    while len(pins) != 4:
        print ('Error. Only 4 digit allowed')
        pins = str(input("Please input pin of your choice: "))
    else:
        pass
    balance = int(input("Please input a value of deposit to start and account: "))
    account = [{"fullname": fullname, "pins": pins, "balance": balance}]
    accounts1 = account.copy()
    accounts.append(accounts1)
    print(accounts)
    customers -= 1
for i in range (len (accounts)):
    if name == accounts[i]['fullname'] and pin == accounts[i]['pins']:
        print('Your current balance is {} PHP'.format(accounts[i]['balance']))
    else:
        print("Account not found")
        break

我认为这是因为数组在追加后会有双括号

1 个答案:

答案 0 :(得分:2)

原始代码有多个错误,包括错误的缩进和缺少但引用的名称列表。

一个 - 至少是一种不错的工作 - 基于您的代码的解决方案是:

accounts = []
names = [] # add the names list
customers = int(input("Number of customers: "))
while len(accounts) != customers:
    fullname = str(input("Input Fullname: "))
    while fullname in names:
        print('Name already exist')
        fullname = str(input("Input Fullname: "))
    else:
        pass
    pins = str(input("Please input pin of your choice: "))
    while len(pins) != 4:
        print('Error. Only 4 digit allowed')
        pins = str(input("Please input pin of your choice: "))
    else:
        pass
    balance = int(
        input("Please input a value of deposit to start and account: "))
    account = {"fullname": fullname, "pins": pins, "balance": balance}
    accounts.append(account)
    names.append(fullname)
    print(accounts)

# Authenticate before staring the loop

name = str(input("Authenticate: Input your full name: "))
pin = str(input("Authenticate: Input your pin: "))
balance = None

for account in accounts:
    if name == account['fullname'] and pin == account['pins']:
        balance = account['balance']
        break
    
if balance == None:
    # then we had no match - otherwise it should be some integer
    print("Account not found")
else:
    print('Your current balance is {} PHP'.format(balance))