Stuck in Bank Teller project

What’s up Codecademy forum!

As you can see by the tittle, I’m working in my Banks Teller project, but I been stuck in a problem for a while. the problem seems to be at the end of the code.

Is there anyone out there who can help me?

checking_balance = 0
savings_balance = 0
 
def check_balance( account_type, checking_balance, savings_balance):
     if account_type == "savings":
         balance = savings_balance
     elif account_type == "checking":
     balance = checking_balance

     else:
         return " Unsuccessful, please enter \'checking\' or \'savings\' "
     balance_statement = "Your " + account_type + " balance is: $" + str(balance)
     return balance_statement
 
print(check_balance("checking",checking_balance,savings_balance))
Your checking balance is: $0

print(check_balance("savings",checking_balance,savings_balance))
Your savings balance is: $0
 
def make_deposit(account_type,amount,checking_balance,savings_balance):
    deposite_status = ""
    if amount > 0:
        if account_type == "savings":
           savings_balance += amount
           deposit_status = "succesful:D"

        elif account_type == "checking":
             checking_balance += amount
             deposit_status = "succsesful :D"

        else:
            return "Unsuccessful, please enter \"checking\" or \"savings\""
    else:
       deposite_status = "unsuccessful, please enter an amount greater than 0"
       deposite_statement = "Deposite of $" + str(amount) + " to your " + account_type + " was: " + deposit_status
  
    print (deposite_statement)
    
    return savings_balance , checking_balance
 
savings_balance, checking_balance = make_deposit("savings",10,check_balance,savings_balance)
Deposite of $10 to your savings was: succesful:D

print(check_balance("savings",check_balance,savings_balance))
Your savings balance is: $10
 
checking_balance, savings_balance = make_deposit("checking",200,checking_balance,savings_balance)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in make_deposit
TypeError: unsupported operand type(s) for +=: 'function' and 'int'

Line 8 should raise an indentation error.

Lines 16 and 19 have syntax errors.

    print (deposite_statement)
UnboundLocalError: local variable 'deposite_statement' referenced before assignment

Is this line in the right place?

deposite_statement = "Deposite of $" + str(amount) + " to your " + account_type + " was: " + deposit_status

Please furnish us with a link to the exercise so we can review the narrative and the instructions.

Sure, I can share the link :smile:

http://localhost:8888/notebooks/bank_teller..ipynb#

and I’m in number 8

We will need the link to the actual exercise page, not the localhost browser.

In the meantime, this line,

savings_balance, checking_balance = make_deposit("savings",10,check_balance,savings_balance)
                                                              ^^^^^^

Should that be checking_balance?

Also,

The values are switched around. Should it be,

checking_balance, savings_balance = make_deposit(

?

It might be helpful to know that your globals are not being updated. Values do not get passed by reference.

Perhaps if you created a dictionary of accounts?

accounts = {
    'checking': 0,
    'savings': 0
}

and then just pass the dictionary instead of the two account balances.

file:///home/chronos/u-b2a559cdbd264cdd5bb1cabe5eb152cee64ffd6c/MyFiles/Downloads/bank_teller…html

i hope this link will help.

and in:

savings_balance, checking_balance = make_deposit(

checking_balance, savings_balance = make_deposit(

I switch the values so they match but still i get error

Is this an exercise? If so, please post a link to the exercise page.

Please post your corrected code so we can have another look. What error are you getting?

It’s a project.

but finally i figured out how to share it.

here is the link:
Bank Teller

1 Like

The step by step instructions are hard not to follow. It means anything I might suggest will go against them. I also don’t have Jupyter Notebook so at best I can check for errors, much like what you have to do. We did solve the error you posted earlier, so that’s something. There is still the issue of the values not updating so there must be an expected return after the deposit.

Does your github page update when you make changes and corrections? If so then we can keep an eye on it.

This code runs without raising any errors. Note the output.

checking_balance = 0
savings_balance = 0
 
def check_balance(account_type, checking_balance, savings_balance):
     if account_type == "savings":
         balance = savings_balance
     elif account_type == "checking":
         balance = checking_balance
     else:
         return "Unsuccessful, please enter 'checking' or 'savings'."
     balance_statement = "Your " + account_type + " balance is: $" + str(balance)
     return balance_statement
 
def make_deposit(account_type, amount, checking_balance, savings_balance):
    deposit_status = ""
    if amount > 0:
        if account_type == "savings":
             savings_balance += amount
             deposit_status = "successful"
        elif account_type == "checking":
             checking_balance += amount
             deposit_status = "successful"
        else:
            return "Unsuccessful, please enter 'checking' or 'savings'."
        deposit_statement = "Deposit of $" + str(amount) + " to your " + account_type + " was: " + deposit_status
    else:
        deposit_status = "Unsuccessful, please enter an amount greater than 0"

    print (deposit_statement)    
    return checking_balance, savings_balance

print(check_balance("checking", checking_balance, savings_balance))
print(check_balance("savings", checking_balance, savings_balance))

checking_balance, savings_balance = make_deposit("savings",10,checking_balance,savings_balance)
print(check_balance("savings",checking_balance,savings_balance))

checking_balance, savings_balance = make_deposit("checking",200,checking_balance,savings_balance)
print(check_balance("checking",checking_balance,savings_balance))
Your checking balance is: $0
Your savings balance is: $0
Deposite of $10 to your savings was: successful
Your savings balance is: $0
Deposite of $200 to your checking was: successful
Your checking balance is: $0

Staring the error in the face…

    return savings_balance , checking_balance

It is switched around. Will amend my code above. New output, below:

Your checking balance is: $0
Your savings balance is: $0
Deposit of $10 to your savings was: successful
Your savings balance is: $10
Deposit of $200 to your checking was: successful
Your checking balance is: $200

Last of all, there is no print out of the deposit_status when zero or less. You could add a print statement right under the assignment in the else clause.

    else:
        deposit_status = "Unsuccessful, please enter an amount greater than 0"
        print (deposit_status)

Off book…

accounts = {
    'checking': 0,
    'savings': 0
}
def check_balance(account, balances):
    balance = balances.get(account, 'NSA')
    return f"Your {account} balance is: ${balance:.2f}" if balance != 'NSA' else "Unsuccessful. Please enter 'checking' or 'savings'."
def make_deposit(account, amount, balances):
    try:
        if amount <= 0: return "Unsuccessful. Please enter an amount greater than 0."
        balances[account] += amount
        return f"Deposit of ${amount:.2f} to your {account} was successful."
    except KeyError:
        return "Unsuccessful. Please enter 'checking' or 'savings'."
    except TypeError:
        return "Unsuccessful. Please enter a positive number."

print (check_balance("savings", accounts))
print (check_balance("checking", accounts))

print (make_deposit("savings", 10, accounts))
print (check_balance("savings", accounts))

print (make_deposit("checking", 200, accounts))
print (check_balance("checking", accounts))

print (make_deposit("checking", 0, accounts))
print (check_balance('windfall', accounts))
print (make_deposit("checking", '100', accounts))
Your savings balance is: $0.00
Your checking balance is: $0.00
Deposit of $10.00 to your savings was successful.
Your savings balance is: $10.00
Deposit of $200.00 to your checking was successful.
Your checking balance is: $200.00
Unsuccessful. Please enter an amount greater than 0.
Unsuccessful. Please enter 'checking' or 'savings'.
Unsuccessful. Please enter a positive number.

This is the sort of refactoring that you will be able to do once you complete the project and have it running to the instruction spec. Brush it aside for now and let’s keep working on the project by the book. Just wanted you to have a sneak peek.

This is really interesting!

Thank you very much for all your help:)

I’ll continue working in the project , like you say by the book, or even start over again. At least is not to much what i have to do.

I’ll keep you updated!

1 Like

You’re welcome! This has been a fun diversion. Don’t mind that I’ve gone overboard…

def error(err, param):
    return {
        'nsa': f"The account {param.__repr__()} was unsuccessful. Please enter 'checking' or 'savings'.",
        'lez': f"The amount {param} was unsuccessful. Please enter an amount greater than 0.",
        'nan': f"The amount {param.__repr__()} was unsuccessful. Please enter a positive number."
    }[err]
def check_balance(account, balances):
    balance = balances.get(account, 'NSA')
    return f"Your {account} balance is: ${balance:.2f}" if balance != 'NSA' else error('nsa', account)
def make_deposit(account, amount, balances):
    try:
        if amount <= 0: return error('lez', amount)    # less than or equal to zero
        balances[account] += amount
        return f"Deposit of ${amount:.2f} to your {account} was successful."
    except KeyError:
        return error('nsa', account)                   # no such account
    except TypeError:
        return error('nan', amount)                    # not a number

accounts = {
    'checking': 0,
    'savings': 0
}

print (check_balance("savings", accounts))
print (check_balance("checking", accounts))

print (make_deposit("savings", 10, accounts))
print (check_balance("savings", accounts))

print (make_deposit("checking", 200, accounts))
print (check_balance("checking", accounts))

print (make_deposit("checking", 0, accounts))
print (make_deposit("checking", '100', accounts))

print (make_deposit('windfall', 100, accounts))
print (check_balance('windfall', accounts))
Your savings balance is: $0.00
Your checking balance is: $0.00
Deposit of $10.00 to your savings was successful.
Your savings balance is: $10.00
Deposit of $200.00 to your checking was successful.
Your checking balance is: $200.00
The amount 0 was unsuccessful. Please enter an amount greater than 0.
The amount '100' was unsuccessful. Please enter a positive number.
The account 'windfall' was unsuccessful. Please enter 'checking' or 'savings'.
The account 'windfall' was unsuccessful. Please enter 'checking' or 'savings'.

Some things in there might yet be new, so the proviso to stay clear still holds. The best example to follow for the time is the one that we had working using mostly your code.

Extra Study

Data types all have their own attributes, though many are commonly named across the spectrum. object.__repr__() is one such attribute. It is a method that knows how to represent the object as its type and how to most accurately cast it to a str.

This is not the same as object.__str__() since it is looking for an informal representation. repr represents it officially. We want strings, lists and dictionaries, etc. to be represented as Python sees them. In the output above we see quotes around the string values, ‘100’ and, ‘windfall’. We didn’t hard code them. That’s what repr did. Drop that attribute and the quotes go away.

Mind that these are string representations, and not necessarily meant as data to send downstream. They would be used primarily in print() statements.

>>> (42).__repr__()
'42'
>>> 

Additional

If you are not yet familiar with f-string method of string formatting then best to take a segue through that topic and stop concatenating forever. Seriously.

Also note that the return line in the check_balance function converts an if statement into an expression. It’s not a ternary, but it comes close. Something else to follow up on.

Forgive me to presume that you have covered dictionaries. If that is not the case then dict.get() will also be unfamiliar to you. Still more to follow up on, but in due course. If you have covered dictionaries, then don’t delay, otherwise wait until it comes up.


When you have more code, do share. I’ll not criticize. I, too, need to learn this from the perspective of the learner, and on book. What I do with it afterwards is fair game.