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