Bank Teller Project

Hello,

This post is regarding the Bank Teller project in Python course.

I’m having an issue with step 7 print(check_balance):

While it should print updated variable “savings_balance” after modifying it in the make_deposit() function, it still displays the starting value.

I’m finding that it has to do with modifying global variables. Can someone help get me through this snag?

Current code:

checking_balance = 0
savings_balance = 0

def check_balance(account_type, checking_balance, savings_balance):
    if account_type == "savings":
        balance = savings_balance
        balance_statement = "Your " + account_type + " balance is " + str(balance)
        return balance_statement
        
    elif account_type == "checking":
        balance = checking_balance
        balance_statement = "Your " + account_type + " balance is " + str(balance)
        return balance_statement
    
    else:
        return "Unsuccessful, please enter \"checking\" or \"savings\""
    
print("3. "+ check_balance("checking", checking_balance, savings_balance))
print("4. "+ check_balance("savings", checking_balance, savings_balance))


def make_deposit(account_type, amount, checking_balance, savings_balance):
    deposit_status = str()
    
    if amount > 0:
        if account_type == "savings":
            savings_balance += amount
            deposit_status = "succussful"
            
        elif account_type == "checking":
            checking_balance += amount
            deposit_status = "succussful"
            
        else:
            deposit_status = "Unsuccessful, please enter \"checking\" or \"savings\""
    
    else:
        deposit_status = "unsuccessful, please enter an amount greater than 0"
        
    deposit_statement = "Deposit of " + str(amount) + " to your " + account_type + " account was " + deposit_status

    print("5.10. " + deposit_statement)
    
    print("5.11. "+ check_balance("checking", checking_balance, savings_balance))
    print("5.11. "+ check_balance("savings", checking_balance, savings_balance))
    

make_deposit("savings", 10, checking_balance, savings_balance)

print("7. " + check_balance("savings", checking_balance, savings_balance))

Current Result:
3. Your checking balance is 0
4. Your savings balance is 0
5.10. Deposit of 10 to your savings account was succussful
5.11. Your checking balance is 0
5.11. Your savings balance is 10
7. Your savings balance is 0

Last line should read “7. Your savings balance is 10” as the instructions are indicating.

Thank you

Hello @mauriceassoulin. Welcome to the forums!
Have you looked at the global keyword? Global keyword in Python - GeeksforGeeks. This may help you with your functions. Also,

why do you have checking_balance and saving_balance here? The quickest way I found was to use global. I’m not sure whether it has been covered in the course?
I hope this helps!

1 Like

Hi @tera5288723178,

I came across that same article prior -

It recommends declaring the variable as global within the function -

# Python program to modify a global 
# value inside a function 
  
x = 15
def change(): 
  
    # using a global keyword 
    global x 
  
    # increment value of a by 5 
    x = x + 5 
    print("Value of x inside a function :", x) 
change() 
print("Value of x outside a function :", x) 

Output:
Value of x inside a function : 20
Value of x outside a function : 20

I attempted declare my variables as global in the same fashion, but with an error:

checking_balance = 0
savings_balance = 0

def make_deposit(account_type, amount, checking_balance, savings_balance):
    global checking_balance
    global savings_balance
    
    deposit_status = str()
    
    if amount > 0:
        if account_type == "savings":
            savings_balance += amount
            deposit_status = "succussful"
            
        elif account_type == "checking":
            checking_balance += amount
            deposit_status = "succussful"
            
        else:
            deposit_status = "Unsuccessful, please enter \"checking\" or \"savings\""
    
    else:
        deposit_status = "unsuccessful, please enter an amount greater than 0"
        
    deposit_statement = "Deposit of " + str(amount) + " to your " + account_type + " account was " + deposit_status
    print("5.10. " + deposit_statement)
    
    print("5.11. " + check_balance("checking", checking_balance, savings_balance))
    print("5.11. " + check_balance("savings", checking_balance, savings_balance))

File "<ipython-input-61-a71bd45b5be2>", line 26
    deposit_status = str()
           ^
SyntaxError: name 'savings_balance' is parameter and global

Thanks for helping out

Maurice

I think the error is pretty informative?

having both a global variable in the function and parameter with the same name, how is python to know which to use?

No. It explains globals, it doesn’t cover recommendations and best practices

1 Like

Hi,

Within each function, I’ve removed them as parameters, declared them as global within

This has solved my issue as you and @ tera5288723178 have explained

Thank you!

1 Like

Using global is something you should generally avoid. Yes, your code might work, but is it good?

1 Like