PYTHON - Loop within a function?

Hello!
I completed an assignment for a course I am taking (writing an ATM script), but I wanted to add some features. The original script gave the user four options, balance, withdrawal, deposit, or quite. Very simple, at the end of the user choice, the program stops. For example, if the user chose deposit, it would call the deposit function, and then end. But I wanted to add a choice at the end of that, giving the user the option to continue (balance, withdrawal…etc).

I set up a test if/else statement in my first function (lines 10 - 17), and it works… but it doesn’t loop to the ‘userchoice’ input on line 38. So I’m a little baffled how to connect, I know I’m missing something, I’m not sure what. I’m not looking for an answer, but maybe just a nudge in the right direction.

Any pointers much appreciated, as always :slight_smile:

import sys

account_balance = float(500.25)
deposit_amount = 0
withdrawal_amount = 0
nextChoice = 'a'

# -------------------------- DEFINE FUNCTIONS - balance, withdrawal, and deposit --------------------------

def balance(account_balance):                                                   # Define balance function
  print("Your current balance: ", account_balance)                              # Prints the current avalible balance
  nextChoice = input("Would you like to (C)ontinue or (E)xit?\n").upper()       # Give user the option to continue or exit
  if (nextChoice == 'C'):                                                       # If user withes to continue, they will be provided with options on how to proceed
    input ("Would you like to check your (B)alance, make a (D)eposit, (W)ithdraw cash, or (E)xit?\n").upper()
  elif (nextChoice == 'E'):
    print("Thank you for banking with us.")
      
def deposit(account_balance, deposit_amount):                                   # Define DEPOSIT function with parameters account_balance and deposit_amount
  deposit_amount = float(input("How much would you like to deposit today?\n"))  # Accept user input for the deposit amount, in float format
  balance = account_balance + deposit_amount                                    # This addition assigns the updated value of the account blance, to the variable 'BALANCE'
  print("Deposit was $%.2f , your new current balance is $%.2f" % (deposit_amount, balance))  # Prints depost amount and account balance

def withdrawal(account_balance, withdrawal_amount):                             # Define WITHDRAWAL function with parameters account_balance and withdrawal_amount
  withdrawal_amount = float(input("How much would you like to withdraw today?\n")) #  Accept user input for the withdrawal amount, in float format
  if withdrawal_amount > account_balance:                                       # Checking to see if the amount requested, is greater than the amount avalible
    print("Insuffient funds, $%.2f is greater than your account balance of $%.2f" % (withdrawal_amount, account_balance)) # If the amount requested is greater than the account balance, there are insuffient funds
  else:                                                                         # Suffient amount of funds are avalible, the function continues
    balance = account_balance - withdrawal_amount                               # Variable 'balance' is assigned to reflect the new avalible balance
    print ("Withdrawal amount was $%.2f, your new current balance is $%.2f" % (withdrawal_amount, balance))  # Prints withdrawal amount and account balance

# Lines 18 and 20 compose a conditional statement with the withdrawal function
# Line 18 => if the requested withdwal amount is greater than the account balance, the conditional statement stops, and prints to the user there are insuffient funds
# Line 20 => if there are Suffient funds avalible, the conditional statement continues, updates the 'balance', and outputs to the user their withdwal amount and new avalible balance

# -------------------------- ACCEPT USER INPUT - D, B, W, or Q --------------------------

# Step ONE =>  Ask user what action they would like to proceed with, user input is accepted and assigned to the variable 'userchoice'
userchoice = input ("Would you like to check your (B)alance, make a (D)eposit, (W)ithdraw cash, or (E)xit?\n").upper()                    

# Step TWO => conditional statement begins based on the value of variable 'userchoice' from user input
# Four branches ustilizing if / elif for DEPOSIT, BALANCE, WITHDRAWAL, EXIT 
if (userchoice == 'D'):                                                         # Accepts input D and proceeds with function 'deposit'
    deposit (account_balance, deposit_amount)                                   # DEPOSIT function is called with parameters 'account_balance' and 'deposit_amount'
    
elif (userchoice == 'B'):                                                       # Accepts input B and proceeds with function 'balance'
  balance (account_balance)                                                     # BALANCE function is called with parameter 'account_balance'
    
elif (userchoice == 'W'):                                                       # Accepts input D and proceeds with function 'withdrawal'
  withdrawal (account_balance, withdrawal_amount)                               # WITHDRAWAL function is called with parameters 'account_balance' and 'withdrawal_amount'

elif (userchoice == 'E'):                                                       # Accepts input E for EXIT
  print("Thank you for banking with us.")                                       # There is no function for EXIT, and therefore the user has a printed message ending their session


Hello @mk39. Have you though about a while loop? This kind of loop loops through the code inside it, and repeats if the condition is true:

var_a = 0
while var_a < 5:
  print(var_a)
  var_a += 1

This will print:

0
1
2
3
4

I hope this helps!

Hello @codeneutrino, I did add a while loop, thanks for the nudge.
All the code works, in regards to looping, however my functions do not return a “historic” account_balance value. …
Say for example you want to check the balance, then you withdrawal, and then decide to withdrawal again, the function doesn’t record the two withdrawals, it treats as a “one time” equation.
Any suggestions on how I would go about recording this? If that makes sense…

new while loop:

userChoice = 'go'                                                               # Setting the variable 'userChoice' to 'go', so we can impliment a while loop

while userChoice != 'E':                                                        # As long as the user does not select 'E' (Exit), the program will keep looping with user choices

# Step ONE =>  Ask user what action they would like to proceed with, user input is accepted and assigned to the variable 'userchoice'
    userChoice = input ("Would you like to check your (B)alance, make a (D)eposit, (W)ithdraw cash, or (E)xit?\n").upper()

# Step TWO => conditional statement begins based on the value of variable 'userchoice' from user input
# Four branches ustilizing if / elif for DEPOSIT, BALANCE, WITHDRAWAL, EXIT
    if (userChoice == 'D'):                                                     # Accepts input D and proceeds with function 'deposit'
        deposit (account_balance, deposit_amount)                               # DEPOSIT function is called with parameters 'account_balance' and 'deposit_amount'

    elif (userChoice == 'B'):                                                   # Accepts input B and proceeds with function 'balance'
        balance (account_balance)                                               # BALANCE function is called with parameter 'account_balance'

    elif (userChoice == 'W'):                                                   # Accepts input D and proceeds with function 'withdrawal'
        withdrawal (account_balance, withdrawal_amount)                         # WITHDRAWAL function is called with parameters 'account_balance' and 'withdrawal_amount'

    elif (userChoice == 'E'):                                                   # Accepts input E for EXIT
        print("Thank you for banking with us.")                                 # There is no function for EXIT, and therefore the user has a printed message ending their session