簡體   English   中英

為什么循環不止於我設置的數字?

[英]Why is my loop not stopping at the number I set?

我正在使用數組和函數用python為銀行應用程序編寫程序。 這是我的代碼:

NamesArray=[]
AccountNumbersArray=[]
BalanceArray=[]
def PopulateAccounts():
    for position in range(5):
        name = input("Please enter a name: ")
        account = input("Please enter an account number: ")
        balance = input("Please enter a balance: ")
        NamesArray.append(name)
        AccountNumbersArray.append(account)
        BalanceArray.append(balance)
def SearchAccounts():
    accounttosearch = input("Please enter the account number to search: ")
    for position in range(5):
        if (accounttosearch==NamesArray[position]):
            print("Name is: " +position)
            break
    if position>5:
        print("The account number not found!")

print("**** MENU OPTIONS ****")
print("Type P to populate accounts")
print("Type S to search for account")
print("Type E to exit")
choice = input("Please enter your choice: ")
while (choice=="E") or (choice=="P") or (choice=="S"):
    if (choice=="P"):
        PopulateAccounts()
    elif (choice=="S"):
        SearchAccounts()
    elif (choice=="E"):
        print("Thank you for using the program.")
        print("Bye")

當用戶輸入“ P”時,應該調用def PopulateAccounts()並調用它,但問題是它不會停止並且用戶必須輸入帳戶名,帳號和帳戶余額。 應該在第5個名稱之后停止。 我該如何解決?

這是因為PopulateAccounts()完成后, while while仍然保持循環,因為choice仍然是P 如果您想問用戶另一個動作,只需再次問他輸入即可。

choice = input("Please enter your choice: ")
while (choice=="E") or (choice=="P") or (choice=="S"):
    if (choice=="P"):
        PopulateAccounts()
    elif (choice=="S"):
        SearchAccounts()
    elif (choice=="E"):
        print("Thank you for using the program.")
        print("Bye")
    choice = input("Please enter another action: ")

另外,我建議您使用無限循環不斷詢問用戶輸入,並在用戶輸入“ E”時中斷輸入,這樣您還可以跟蹤無效輸入。

while True:
    choice = input("Please enter your choice: ")
    if choice == "P":
        PopulateAccounts()
    elif choice == "S":
        SearchAccounts()
    elif choice == "E":
        print("Thank you for using the program.")
        print("Bye")
        break
    else:
        print("Invalid action \"{}\", avaliable actions P, S, E".format(choice))
    print()

在循環開始之前,您的代碼僅要求用戶選擇一次。 因為它永遠不會改變,所以該循環將堅持用戶無限次迭代的選擇。

choice = input("Please enter your choice: ")
while (choice=="E") or (choice=="P") or (choice=="S"):
    if (choice=="P"):
        PopulateAccounts()
    elif (choice=="S"):
        SearchAccounts()
    elif (choice=="E"):
        print("Thank you for using the program.")
        print("Bye")
    # here at the end of this loop, you should 
    # get the user to enter another choice for the next 
    # iteration. 

您的while循環沒有計數器使其停止在第5個名稱處,並且position僅在執行其所在的函數期間存在。而且, position永遠不會大於4。range range(5)從0開始並在以下位置結束4。

您的for循環很好。 問題是您的while循環正在重復。 因此,在調用PopulateAccounts()之后,它會在for循環中運行5次后正確完成,但是由於choice仍然等於“ P”(用戶首次輸入該參數后並未更改),因此您仍然留在while循環,這意味着PopulateAccounts()將被PopulateAccounts()調用。 您可以通過在“ while”行之后添加一個附加語句,例如“ print("Hey, we're at the top of the While loop!")來驗證這一點。

如果用戶選擇“ E”,請嘗試使用明顯的中斷來重寫while循環:

while True:
    if (choice=="P"):
        PopulateAccounts()
    elif (choice=="S"):
        SearchAccounts()
    elif (choice=="E"):
        print("Thank you for using the program.")
        print("Bye")
        quit()
    choice = input("Please enter either P, S or E: ")

請注意,如果用戶鍵入了除“ P”,“ S”或“ E”以外的其他內容,底部的此額外輸入也會很方便地顯示。 您可能還需要考慮將.upper()添加到choice檢查中,以使其不區分大小寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM