简体   繁体   English

为什么“找不到帐户”行不起作用? 还是编码的初学者,并尝试过if和else语句

[英]Why won't the “Account not found” line not work? Still a beginner at coding and have tried if and else statements

Basic Bank App 基本银行应用

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()

This area is the area that is wrong 该区域是错误的区域

After populating(creating) accounts ,searching for accounts, and entering an account ID that is not in the list of Accounts, it should display "Account not found. Please try again". 填充(创建)帐户,搜索帐户并输入不在“帐户”列表中的帐户ID后,它应显示“找不到帐户。请重试”。 However when I run it, it shows an error instead of displaying the prompt. 但是,当我运行它时,它显示一个错误而不是显示提示。 Any suggestions or things I am doing wrong? 有什么建议或我做错了什么吗? I am currently learning python. 我目前正在学习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()

Since you haven't provided the error, I'm going to guess what the error is: 由于您尚未提供错误,因此我将猜测错误是什么:

Inside your searchArray function, you increment position up to 10 times. searchArray函数中,您最多可以将position增加10次。 If you don't have 10 accounts in Accounts and you are looking for an accountIDstr that isn't in Accounts , you will try to index outside the range of the list. 如果你没有在10个账户Accounts和你正在寻找一个accountIDstr不在Accounts ,你会尝试索引列表的范围之外。


Short term fix is to say something like this: 短期修复是这样说的:

for aNumber in range(len(Accounts)):

But I'd highly recommend looking into some better and more Pythonic solutions. 但我强烈建议您研究一些更好的Pythonic解决方案。 See below for one potential place to start. 请参阅以下一个可能的起点。


Longer term: 长期:

It's definitely a good start, though I would seriously consider restructuring your program. 尽管我会认真考虑重组您的程序,但这绝对是一个好的开始。 You can still format it with classes or functions, but it's not a good idea to have the functions make an infinite amount of calls to each other. 您仍然可以使用类或函数对其进行格式化,但是让函数之间进行无限次调用并不是一个好主意。

If run for long enough, you will have problems with recursing too deep with this code. 如果运行足够长的时间,则此代码的递归深度可能会遇到问题。 For example, your choicesMenu function calls itself. 例如,您的choicesMenu函数将自行调用。 I see what you are doing (in order to loop it), but you should really be using a while loop here. 我知道您在做什么(以便循环播放),但是您实际上应该while这里使用while循环

And here's why: When choicesMenu hasn't hit a return or implicitly returned, and makes a call to some other function, Python has to remember the state of choicesMenu so when it comes back from that other function, it will know where it left off. 原因如下:当choicesMenu尚未return或隐式返回并调用其他函数时,Python必须记住choicesMenu的状态,因此当它从其他函数返回时,它将知道在哪里停止。 If choicesMenu just keeps calling itself, it will fill up that memory. 如果choicesMenu只是不断调用自身,它将填满该内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM