Bank Teller: Question about Withdrawal

Hello everyone,

I am doing the Bank Teller jupyter notebook project and I have a question.

I had some trouble at first with the step where I was supposed to determine that the amount of the withdrawal was less than the balance of the account. When I first tried to do the withdrawal section of the project (question 10) , I wrote the following code:

#my original withdrawal function
def make_withdrawal(account_type,amount,checking_balance,savings_balance):
    withdrawal_status = ""
    fail = "unsuccessful, please enter amount less than balance"
    if account_type == "savings":
        if amount > savings_balance:
            withdrawal_status = fail
        else:
            savings_balance -= amount
            withdrawal_status = "successful"     
    elif account_type == "checking":
        if amount > checking_balance:
            withdrawal_status = fail
        else:
            checking_balance -= amount
            withdrawal_status = "successful"     

I got an error message saying that the syntax wasn’t correct. After spending some time trying to figure out what I did incorrectly, I checked the answer. They write the following code instead:

#withdrawal function from solution
def make_withdrawal(account_type,amount,checking_balance,savings_balance):
    withdrawal_status = ""
    fail = "unsuccessful, please enter amount less than balance"
    if account_type == "savings":
        if amount <= savings_balance:
            savings_balance -= amount
            withdrawal_status = "successful"
        else:
            withdrawal_status = fail
    elif account_type == "checking":
        if amount <= checking_balance:
            checking_balance -= amount
            withdrawal_status = "successful"
        else:
            withdrawal_status = fail

To my mind, checking whether amount > savings_balance then fail is the same as checking whether amount <= savings_balance. I couldn’t figure out why there were error messages for my original code. When I tried my original method again today, the code worked which makes me think that perhaps I just had a formatting issue when I tried to do it yesterday.

This is a long introduction for a simple question, but I guess my question is:
Are these two ways of coding this functionally the same or is one preferable for some reason?

Thanks for your help.

Hello! What exactly was the error message you received?

The logic looks okay. Did you use the exact phrases given in the instruction?

1 Like

Thank you both for your help. The error was something about the syntax not being correct. I tried to get the exact same error again but when I did it again today it worked. I must have just had a typo or something yesterday that I kept missing. I was curious as to whether the logic in my original solution looked ok and it sounds like it was fine.

Thanks again.

3 Likes