I completed an assignment for a scripting class I'm taking, and it worked fine, but I wanted to take it a step further, which has sent me down a rabbit hole. The script is for an ATM transaction.
I added a while loop to give the user additional options, instead of just ending the program. But I found the my account_balance variable remained equal to the default value set initially (in this case 500.25). I've been trying to find ways to make this update as the user goes through the program, in theory it should go as follows:
- User checks balance = $500.25 --------------> balance(account_balance)
- User makes deposit = $500.00 ---------------> deposit(account_balance, amount)
- User checks balance again = $1,000.25 ---> program always returns with $500.25
To try and make this work, I made another function transactionGo(pin), and set all the other functions inside it, hoping this would update the variable account_balance, that didn't work either, and in fact caused more problems, I feel I'm going down a road I shouldn't.
import sys
# -------------------------- DECLARE variables for balance, deposit, and withdrawal --------------------------
account_balance = float(500.25) # Starting balance indicated by Codio
#balance = 100
amount = 0
#deposit_amount = 0 # Declare variable 'deposit_amount'
#withdrawal_amount = 0 # Declare variable 'withdrawal_amount'
pin = ''
# -------------------------- DEFINE FUNCTIONS - balance, withdrawal, and deposit -----------------------------
def transactionGo (pin):
pin = int(input("Please enter your 5 digit pin number:\n"))
def balance(account_balance): # Define balance function
print("Your current balance is $%.2f" % (account_balance)) # Prints the current available balance
def deposit(account_balance, amount): # Define DEPOSIT function with parameters account_balance and amount
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 + amount # This addition assigns the updated value of the account balance, to the variable 'account_balance'
print("Deposit was $%.2f , your new current balance is $%.2f" % (amount, balance)) # Prints deposit amount and account balance
return balance # Return records the new value of account_balance to reflect accordingly in other transactions
def withdrawal(account_balance, amount): # Define WITHDRAWAL function with parameters account_balance and withdrawal_amount
amount = float(input("How much would you like to withdraw today?\n")) # Accept user input for the withdrawal amount, in float format
if amount > account_balance: # Checking to see if the amount requested, is greater than the amount available
print("Insufficient funds, $%.2f is greater than your account balance of $%.2f" % (amount, account_balance)) # If the amount requested is greater than the account balance, there are insufficient funds
else: # Sufficient amount of funds are available, the function continues
balance = account_balance - amount # Variable 'balance' is assigned to reflect the new available balance
print ("Withdrawal amount was $%.2f, your new current balance is $%.2f" % (amount, balance)) # Prints withdrawal amount and account balance
return balance # Return records the new value of balance to reflect accordingly in other transactions
# Lines 18 and 20 compose a conditional statement with the withdrawal function
# Line 18 => if the requested withdrawal amount is greater than the account balance, the conditional statement stops, and prints to the user there are insufficient funds
# Line 20 => if there are sufficient funds available, the conditional statement continues, updates the 'balance', and outputs to the user their withdrawal amount and new available balance
# ------------------------------------ ACCEPT USER INPUT - D, B, W, or Q -------------------------------------
userAccess = input ("Welcome to Tom & Kate Banking, if you would like to sign into your account, please press (C)ontinue, or (E)xit\n").upper()
if userAccess == 'C':
transactionGo (pin)
userChoice = 'go' # Setting the variable 'userChoice' to 'go', so we can implement a while loop
# Step ONE => Create a WHILE loop to offer the user additional options after they have completed a transaction
while userChoice != 'E': # As long as the user does not select 'E' (Exit), the program will keep looping with user choices
# Step TWO => 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 THREE => conditional statement begins based on the value of variable 'userchoice' from user input
# Four branches utilizing if / elif for DEPOSIT, BALANCE, WITHDRAWAL, EXIT
if (userChoice == 'D'): # Accepts input D and proceeds with function 'deposit'
deposit (account_balance, amount) # DEPOSIT function is called with parameters 'balance' and 'amount'
elif (userChoice == 'B'): # Accepts input B and proceeds with function 'balance'
balance (account_balance) # BALANCE function is called with parameter 'balance'
elif (userChoice == 'W'): # Accepts input D and proceeds with function 'withdrawal'
withdrawal (account_balance, amount) # WITHDRAWAL function is called with parameters 'balance' and '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
else:
print("We hope to see you again soon, have a nice day!")
When I run this, the first function runs - transactionGo(pin), but when I go into the other transaction functions, IDLE tells me my variables are undefined.
global amount. Otherwise assigning to it will assign to a local variable, leaving the global variable unchanged.classand make it an attribute of theclass.