0

I'm making a ATM-type program in which I have to ask the user if they want to deposit, withdraw, or check their balance in their savings or checking account, but only if the pin they enter is 1234. I've been instructed to use global variables and initialize savings and checking as 0. All of my functions are working properly, but when the program loops again the savings and checking balances are still 0, even when the user has just deposited money into either account. I'm not sure which part of my code is messing this up, but any help is greatly appreciated.

#Create global variables
Checking = 0
Saving = 0

def main():
    pin_number = input("Please enter your pin number ")
    stop = False

    while not is_authorized(pin_number) and stop!= True:

        if pin_number == "0":
            stop = True
        if pin_number == "1234":
            stop = False  

    if stop != True:
        while True:
            choice = display_menu()
            if choice == 1:
                deposit()
            elif choice == 2:
                withdraw()
            elif choice == 3:
                check_balance()

def is_authorized (pin_number):
    if pin_number == "1234":
        return True 
    else:
        return False

def display_menu():
    print('1) Deposit')
    print('2) Withdraw')
    print('3) Check amount')
    choice = int(input("Please enter the number of your choice: "))
    return choice 


def deposit(): 
    depositing=str(input("Would you like to deposit into your savings 
or checking? "))
    if depositing == "savings":
        depo = float(input("How much would you like to deposit? "))
        new_savings = Saving + depo
        print ("Your new balance is" +str (new_savings))
    elif depositing == "checkings":
        depo = input(int("How much would you like to deposit? "))
        new_checking = Checking + depo
        print ("Your new balance is" +str (new_checking))

def withdraw():
    print ("Your savings account balance is " +str (Saving))
    print ("Your checkings account balance is " +str (Checking))
    withdrawing=str(input("Would you like to withdraw from your checking or savings? "))
    if withdrawing == "checking":
        withdraw = int(input("How much would you like to withdraw? "))
        checking2= Checking - withdraw
        print ("Your new balance is" +str (checking2))
    elif withdrawing == "savings":
        withdraw = int(input("How much would you like to withdraw? "))
        savings2= Saving - withdraw
        print ("Your new balance is" +str (savings2))

def check_balance():
    checkbalance= input("Would you like to check your savings or checking? ")
    if checkbalance == "savings":
        print (Saving)
    elif checkbalance == "checking":

        print ("Your balance is $" +str (Checking))
main()
Haley
  • 39
  • 6
  • 4
    "_when the program loops again the savings and checking balances are still 0_" You aren't actually modifying your `Saving` variable here `new_savings = Saving + depo`. Same for the other actions as well. If you don't give `Saving` a new value, it will always be 0. – takendarkk Oct 12 '18 at 18:28
  • I switched new_savings to Saving and the following error pops up: Saving = Saving + depo UnboundLocalError: local variable 'Saving' referenced before assignment – Haley Oct 12 '18 at 18:34
  • And I am sure you are not the first person to see that error message so you can find info about it on this site. For example, I googled the error and found https://stackoverflow.com/questions/423379/using-global-variables-in-a-function. No promise it solves the issue but it makes it clear that you can research this on your own. – takendarkk Oct 12 '18 at 18:36
  • Okay, I was able to fix that problem but my program still isn't looping properly-- the balances are updated but when it loops it goes to the show menu function without asking the user to input their pin again – Haley Oct 12 '18 at 18:51
  • Because that part is not inside the loop so it will not be repeated. If you want it repeated, put it in the loop which is the thing that repeats. – takendarkk Oct 12 '18 at 18:53

1 Answers1

0

In order to modify values Saving and Checking in your functions you have to declare them global inside the function, not the use of global is not the most optimal, as far as I know, but it seems that is your task at hand. Your while loop can be simplified, by using while True and break. Along the way I found a few small things I touched up, I would also consider changing Saving to Savings since that is the keyword you are accepting as a response, makes things easier for you.

Checking = 0
Saving = 0

def main():

    while True:
        pin_number = input("Please enter your pin number ")   
        while pin_number not in ('0','1234'):
            pin_number = input("Please enter your pin number ")

        if pin_number == "0":
            break

        elif pin_number == "1234":
            choice = display_menu()
            if choice == 1:
                deposit()
            elif choice == 2:
                withdraw()
            elif choice == 3:
                check_balance()

def is_authorized (pin_number):
    if pin_number == "1234":
        return True 
    else:
        return False

def display_menu():
    print('1) Deposit')
    print('2) Withdraw')
    print('3) Check amount')
    choice = int(input("Please enter the number of your choice: "))
    return choice 

def deposit(): 
    global Checking, Saving
    depositing=str(input("Would you like to deposit into your savings or checking? "))
    if depositing == "savings":
        depo = float(input("How much would you like to deposit? "))
        Saving += depo # use += here
        print ("Your new balance is " +str (Saving))
    elif depositing == "checking": # changed to checking 
        depo = int(input("How much would you like to deposit? "))
        Checking += depo 
        print ("Your new balance is " +str (Checking))

def withdraw():
    global Checking, Saving
    print ("Your savings account balance is " +str (Saving))
    print ("Your checkings account balance is " +str (Checking))
    withdrawing=str(input("Would you like to withdraw from your checking or savings? "))
    if withdrawing == "checking":
        withdraw = int(input("How much would you like to withdraw? "))
        Checking -= withdraw
        print ("Your new balance is " +str (Checking))
    elif withdrawing == "savings":
        withdraw = int(input("How much would you like to withdraw? "))
        Saving -= withdraw
        print ("Your new balance is " +str (Saving))

def check_balance():
    global Checking, Saving
    checkbalance= input("Would you like to check your savings or checking? ")
    if checkbalance == "savings":
        print (Saving)
    elif checkbalance == "checking":
        print ("Your balance is $" +str (Checking))
main()
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20