1 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊
概括
簡單地說,這是因?yàn)槊看蝫ithdrawal調(diào)用該函數(shù)時(shí),它都會(huì)迭代所有帳戶,并與第一個(gè)具有足夠高余額的帳戶進(jìn)行交易。由于“Bryan Somto”是第一個(gè)帳戶,因此交易始終通過該帳戶進(jìn)行。修改該withdrawal函數(shù)以僅接受用于進(jìn)行交易的特定帳戶。
解釋
當(dāng)您調(diào)用該withdrawal函數(shù)時(shí),您應(yīng)該只傳遞用戶正在進(jìn)行交易的特定帳戶。因此,不要調(diào)用 ,而是withdrawal(accounts)調(diào)用withdrawal(account)。然后僅將該特定帳戶傳遞給該函數(shù)。
def withdrawal(account):
amount = int(input("\nEnter amount to withdraw: "))
if account.balance > amount:
account.balance -= amount
print("\nTransaction successful, your new balance is ${}".format(account.balance))
# New transaction
new = input("\nNew transaction? YES/NO?: ")
if new.lower() == "yes":
return withdrawal(account)
print("\nTake your card {}. Thank you for banking with us.".format(account.name))
else:
print("\nTransaction failed due to insufficient funds.")
這里,該withdrawal函數(shù)僅處理特定帳戶。
如果您也修改您的功能,那就最好了validation。因?yàn)槟壳?,如果多個(gè)人擁有相同的 PIN,則無法正常工作。它應(yīng)首先輸入帳戶持有人的姓名,然后輸入 PIN。然后它應(yīng)該檢查兩者是否匹配。
像這樣:
def validation(accounts):
name = input("Enter your name: ")
for account in accounts:
# Checking account name
if account.name == name:
pin = int(input("Enter 4 digits PIN: "))
# Checking PIN length
if len(str(pin)) != 4:
print("\nInvalid PIN.\n")
return try_again(accounts)
# Checking PIN
if account.pin == pin:
print("\nWelcome! {}, your account balance is ${}".format(account.name, account.balance))
return withdrawal(account)
else:
print("\nThe PIN is incorrect")
return try_again(accounts)
else:
print("\nThere is no account with that name.")
return try_again(accounts)
這里它也只檢查一次引腳的長度。在原始代碼中,它每次都會(huì)檢查長度,這是不必要的。
if在函數(shù)的第一塊中,如果改為try_again會(huì)更好。您不需要換行符,它可能會(huì)導(dǎo)致錯(cuò)誤。return "\n" + validation(accounts)return validation(accounts)
在旁邊
關(guān)于檢查賬戶名:
標(biāo)準(zhǔn)做法是使用一個(gè)絕對(duì)唯一的帳號(hào)/ID,那么即使兩個(gè)人同名,它仍然有效。它也更好,因?yàn)橥ǔ]斎霂ぬ?hào)比輸入長名稱更容易。在這個(gè)例子中,如果兩個(gè)人有相同的名字,它將選擇列表中的第一個(gè)accounts,而永遠(yuǎn)不會(huì)到達(dá)第二個(gè)。
添加回答
舉報(bào)