Home > Back-end >  How to make loop that continues until user is finished with selections?
How to make loop that continues until user is finished with selections?

Time:12-11

I am trying to create a banking program. I want the user to select something (we will say 'D' for deposit) and then when the code completes and the user get their new account total returned, I want to start over at the Useraction and have that happen continuously until the user is done with all of their transactions.

print('What would you like to do?')

UserAction = input("Type 'D' to make deposit\nType 'W' to make a withdrawal\nType 'B' to check your balance: ")

while True:
    if UserAction.upper() == 'D':
        print('Your current balance is', Userbal, 'Dollars' ) ; sleep(1)
        DepositAmount = input('How much would you like to deposit? (include cents too): ')
        pro()
        print('Your balance has been updated!')
        UserCash = float(Userbal)   float(DepositAmount)
        Userbal = '{:.2f}'.format(UserCash)
        print('Your new balance is', Userbal, 'Dollars' )  
        UserAction 
        

    if UserAction.upper() == 'W':
        print('Your current balance is', Userbal, 'Dollars' ) ; sleep(1)
        WithdrawAmount = input('How much would you like to withdraw? (include cents too): ')
        pro()
        print('Your balance has been updated!')
        UserCash = float(Userbal) - float(WithdrawAmount)
        Userbal = '{:.2f}'.format(UserCash)
        print('Your new balance is', Userbal, 'Dollars' )  
        UserAction
        

    if UserAction.upper() == 'B':
        print('Your current balance is', Userbal, 'Dollars' )
        UserAction

CodePudding user response:

Without the actual implementation of deposit , withdrawal and balance check ...

def PromptAction():
    UserAction = 'P' #priming the variable
    while UserAction != 'E':
        UserAction = input('Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:')
        if UserAction.upper() == 'D':
            print('All Actions for deposit.')
        elif UserAction.upper() == 'W':
            print('All Actions for withdrawal.')
        elif UserAction.upper() == 'B':
            print('All actions for checking balance.')
        elif UserAction.upper() == 'E':
            print('Exiting...')
        else:
            print('{0} is not a valid input.'.format(UserAction))

Execution:

>>> PromptAction()
Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:D
All Actions for deposit.
Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:W
All Actions for withdrawal.
Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:B
All actions for checking balance.
Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:X
X is not a valid input.
Enter your choice(D-deposit,W-withdraw,B-balance,E-exit:E
Exiting...
>>> 

CodePudding user response:

You need a nested while loop to perform the the task. After each transaction, you can prompt the user if they want to, for example, make another deposit.

account = [100]
print("WELCOME TO PYTHON BAMK")
while True:
    choice = int(input("[1] Balance\n[2] Deposit\n[3] Withdrawl\n[4] Exit\nEnter choice: "))
    if choice == 1:
        while True:
            print(f'Your balance: {account}')
            break
    elif choice == 2:
        while True: #use nested loop for each block 
            amount = int(input("Enter amount: "))
            account.append(amount)
            print(f"Deposit successful!\nBalance: {sum(account)}")

            another = input("Make another deposit? Y/N: ").capitalize().strip() #prompt user to choose 
            if another == 'Y':
                another = True
            else:
                break
    elif choice == 3:
        pass
    else:
        print("Thank you for using Python Bank.")
        break

CodePudding user response:

Add input into the first line of the cyclic body

print('What would you like to do?')
while True:
    UserAction = input("Type 'D' to make deposit\nType 'W' to make a withdrawal\nType 'B' to check your balance: ")

    if UserAction.upper() == 'D':
        print('Your current balance is', Userbal, 'Dollars' ) ; sleep(1)
        DepositAmount = input('How much would you like to deposit? (include cents too): ')
        pro()
        print('Your balance has been updated!')
        UserCash = float(Userbal)   float(DepositAmount)
        Userbal = '{:.2f}'.format(UserCash)
        print('Your new balance is', Userbal, 'Dollars' )  
        UserAction 
    

    if UserAction.upper() == 'W':
        print('Your current balance is', Userbal, 'Dollars' ) ; sleep(1)
        WithdrawAmount = input('How much would you like to withdraw? (include cents too): ')
        pro()
        print('Your balance has been updated!')
        UserCash = float(Userbal) - float(WithdrawAmount)
        Userbal = '{:.2f}'.format(UserCash)
        print('Your new balance is', Userbal, 'Dollars' )  
        UserAction
    

    if UserAction.upper() == 'B':
        print('Your current balance is', Userbal, 'Dollars' )
        UserAction
  • Related