H. Khan
H. Khan

Reputation: 51

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

I am writing a program in python for a banking application using arrays and functions. Here's my code:

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

When the user enters "P" it is supposed to call to def PopulateAccounts() and it does, but the problem is that it doesn't stop and the user keeps having to input account name, account number, and account balance. It is supposed to stop after the 5th name. How do I fix this?

Upvotes: 0

Views: 275

Answers (4)

Bill M.
Bill M.

Reputation: 1548

Your for loop is fine. The problem is that your while loop is repeating. So after PopulateAccounts() is called, it correctly finishes after running through the for loop 5 times, but since choice is still equal to "P" (this hasn't been changed after the user first enters it), you still remain in the while loop, which means PopulateAccounts() will be called again and again. You can verify this by sticking an additional statement like "print("Hey, we're at the top of the While loop!")" after the "while" line.

Try rewriting your while loop with an explicit break if the user selects "E":

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: ")

Note that this extra input at the bottom also conveniently appears if the user typed something else besides "P", "S", or "E". You may also want to consider adding .upper() to the choice checks to make it case insensitive.

Upvotes: 0

Filip Młynarski
Filip Młynarski

Reputation: 3612

It's because after PopulateAccounts() finishes while loop keeps iterating because choice is still P. If you want to ask user for another action simply ask him again for input.

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: ")

Also I'd recommend you use infinite loop to keep asking user for inputs, and break out of it when user enters 'E', this way you could also track invalid inputs.

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

Upvotes: 3

Ctrl S
Ctrl S

Reputation: 1103

Your while loop has no counter to make it stop at the 5th name, and position only exists during the execution of the function that it is in. Also, position will never be greater than 4. range(5) starts at 0 and ends at 4.

Upvotes: 0

bgporter
bgporter

Reputation: 36504

Your code asks for the user's choice only once -- before the loop begins. Because it never changes, that loop will stick with the user's choice for an infinite number of iterations.

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. 

Upvotes: 0

Related Questions