为什么循环不止于我设置的数字?

时间:2018-11-28 20:40:19

标签: python

我正在使用数组和函数为银行应用程序编写python程序。这是我的代码:

Started:  3:28:09 PM
Execution ID: 41165.
To view the details for the execution, right-click on the Integration Services Catalog, and open the [All Executions] report
Started:  3:28:09 PM
Finished: 3:28:09 PM
Elapsed:  0.172 seconds

当用户输入“ P”时,它应该调用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") 并且可以,但是问题在于它不会停止并且用户必须输入帐户名,帐号和帐户平衡。应该在第5个名称之后停止。我该如何解决?

4 个答案:

答案 0 :(得分:3)

这是因为在PopulateAccounts()完成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()

答案 1 :(得分:0)

您的代码仅在循环开始之前要求用户选择一次。因为它永远不会改变,所以该循环将坚持用户无限次迭代的选择。

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. 

答案 2 :(得分:0)

您的while循环没有计数器使其停止在第5个名称,并且position仅在执行其所在的函数期间存在。而且,position永远不会大于4。range(5)从0开始到4结束。

答案 3 :(得分:0)

您的for循环很好。问题是您的while循环正在重复。因此,在调用PopulateAccounts()之后,它在运行for循环5次后正确完成了,但是由于choice仍然等于“ P”(用户使用后未更改)首先进入它),您仍然停留在while循环中,这意味着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检查中以使其不区分大小写。