[英]Why won't the “Account not found” line not work? Still a beginner at coding and have tried if and else statements
基本銀行應用
print('------------------------------------------------')
print("Welcome to Anthony's Bank App! ")
print('------------------------------------------------')
Names=[]
Accounts=[]
Balance=[]
def popArray():
print("---------------------------------------------")
for aNumber in range (5):
AccountName = input("Enter a name for the account (or enter E to go to menu):" )
if AccountName == "E" or AccountName == "e":
choicesMenu()
else:
Names.append(AccountName)
AccountID = input("Enter account ID number: ")
Accounts.append(AccountID)
BalanceVal = (int(input("Enter an account balance: ")))
Balance.append(BalanceVal)
choicesMenu()
def searchArray():
position = 0
accountIDstr=input("Please enter your account ID number: ")
for aNumber in range(10):
if (accountIDstr == Accounts[position]):
break
if (accountIDstr != Accounts[position]):
position += 1
if (accountIDstr == Accounts[position]):
print("---------------------------------------------")
print("Account Holder: ", Names[position])
print("Balance: $", Balance[position])
choicesMenu()
else:
print("Account not found. Please try again...")
choicesMenu()
def depArray():
position = 0
accountIDstr=input("Please enter your account ID number: ")
for aNumber in range(5):
if (accountIDstr == Accounts[position]):
break
if (accountIDstr != Accounts[position]):
position += 1
balanceAmt = Balance[position]
print("---------------------------------------------")
print("Account Holder: ", Names[position])
print("Balance: $", balanceAmt)
depositAmt=int(input("Enter deposit amount: "))
NewBalance = int(balanceAmt) + depositAmt
total = NewBalance
Balance[position]=total
print("---------------------------------------------")
print("New Balance: $", Balance[position])
choicesMenu()
def wdrawArray():
position=0
accountIDstr=input("Please enter your account ID number: ")
for aNumber in range(5):
if (accountIDstr == Accounts[position]):
break
if (accountIDstr != Accounts[position]):
position += 1
balanceAmt = Balance[position]
balanceVal = int(balanceAmt)
print("---------------------------------------------")
print("Account Holder: ", Names[position])
print("Balance: $", balanceVal)
withdrawAmt=int(input("Enter withdraw amount: "))
if withdrawAmt > balanceVal:
print("------------------------------------------------")
print("Insufficient Funds. Try again...")
choicesMenu()
else:
NewBalance = balanceVal - withdrawAmt
total = NewBalance
Balance[position]=total
print("---------------------------------------------")
print("New Balance: $", Balance[position])
choicesMenu()
def exitArray():
print("------------------------------------------------")
print("Thank you for using Anthony's Bank App. Have a nice day! ")
print(":)")
quit()
def choicesMenu():
print("------------------------------------------------")
print("Enter P to populate accounts.")
print("Enter S to search for an account.")
print("Enter E to exit the app.")
print("Enter D to deposit funds into an account.")
print("Enter W to withdraw funds from an account.")
inputStr = input("Please pick a choice: ")
if inputStr == "P" or inputStr == "p":
popArray()
if inputStr == "S" or inputStr == "s":
searchArray()
if inputStr == "E" or inputStr == "e":
exitArray()
if inputStr == "D" or inputStr == "d":
depArray()
if inputStr == "W" or inputStr == "w":
wdrawArray()
else:
print("Invalid choice. Try again...")
choicesMenu()
choicesMenu()
填充(創建)帳戶,搜索帳戶並輸入不在“帳戶”列表中的帳戶ID后,它應顯示“找不到帳戶。請重試”。 但是,當我運行它時,它顯示一個錯誤而不是顯示提示。 有什么建議或我做錯了什么嗎? 我目前正在學習python。
def searchArray():
position = 0
accountIDstr=input("Please enter your account ID number: ")
for aNumber in range(10):
if (accountIDstr == Accounts[position]):
break
if (accountIDstr != Accounts[position]):
position += 1
if (accountIDstr == Accounts[position]):
print("---------------------------------------------")
print("Account Holder: ", Names[position])
print("Balance: $", Balance[position])
choicesMenu()
else:
print("Account not found. Please try again...")
choicesMenu()
由於您尚未提供錯誤,因此我將猜測錯誤是什么:
在searchArray
函數中,您最多可以將position
增加10次。 如果你沒有在10個賬戶Accounts
和你正在尋找一個accountIDstr
不在Accounts
,你會嘗試索引列表的范圍之外。
短期修復是這樣說的:
for aNumber in range(len(Accounts)):
但我強烈建議您研究一些更好的Pythonic解決方案。 請參閱以下一個可能的起點。
長期:
盡管我會認真考慮重組您的程序,但這絕對是一個好的開始。 您仍然可以使用類或函數對其進行格式化,但是讓函數之間進行無限次調用並不是一個好主意。
如果運行足夠長的時間,則此代碼的遞歸深度可能會遇到問題。 例如,您的choicesMenu
函數將自行調用。 我知道您在做什么(以便循環播放),但是您實際上應該while
這里使用while
循環 。
原因如下:當choicesMenu
尚未return
或隱式返回並調用其他函數時,Python必須記住choicesMenu
的狀態,因此當它從其他函數返回時,它將知道在哪里停止。 如果choicesMenu
只是不斷調用自身,它將填滿該內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.