2

First, I will warn you, I am new at this so please bear with me. I created the following program (for a class, of course) and everything works except that the withdrawal calculation spits out a negative number when the value shouldn't be negative. Can you guys see what I'm doing wrong?

Thanks in advance

#define the main def main(): name=input('Enter the customer\'s name: ') account_id=input('Enter the account ID: ') code=input('Enter the transaction code:') previous_balance=float(input('Enter the previous balance: ')) transaction_amount=float(input('Enter the transaction amount: ')) if code == "w" or code == "W": process_withdrawal (transaction_amount, previous_balance) else: if code == "d" or code == "D": process_deposit (transaction_amount, previous_balance) else: process_invalid_transaction_code (previous_balance) #define process withdrawal def process_withdrawal (previous_balance, transaction_amount): if previous_balance >= transaction_amount: print('You have entered an invalid transaction amount') balance=previous_balance print_balance (balance) else: balance=previous_balance-transaction_amount print_balance (balance) #define process deposit def process_deposit (previous_balance, transaction_amount): balance=previous_balance+transaction_amount print_balance (balance) #define invalid transaction code def process_invalid_transaction_code (previous_balance): print('You have entered an invalid transaction code.') balance=previous_balance print_balance (balance) #define print balance def print_balance(balance): print('Your current balance is :', format(balance, '.2f')) main() 
2
  • if code == "w" or code == "W" is the same as if code in ("w", "W"). Meaning, if the code is in that tuple. Not necessarily related to your question, but it's a good trick to know Commented Feb 16, 2012 at 23:37
  • Could you please include some input/output? Like, what is the output when you enter a few specific values as input? Commented Feb 16, 2012 at 23:37

3 Answers 3

3

Your call to process_withdrawal has the first argument as transaction_amount and the second as previous_balance, but the function declaration has previous_balance as the first argument and transaction_amount as the second.

Try this:

if code == "w" or code == "W": process_withdrawal(previous_balance, transaction_amount) 
Sign up to request clarification or add additional context in comments.

1 Comment

Incidentally, problems like this can be made less frequent if you use classes (not the school kind, the Python class). If you defined an Account class, with deposit and withdraw methods (each taking an amount) and a balance data member, none of this would likely have happened.
1

You are passing the arguments backwards.

process_withdrawal (transaction_amount, previous_balance)

and

def process_withdrawal (previous_balance, transaction_amount):

Comments

0

I think your if statement within process withdrawl should be

if previous_balance < transaction_amount: 

2 Comments

i tried that originally and it was all messed up (i.e. it computed the invalid option and gave the error for the valid one). i totally have no idea why.
check out the other answers. you got your arguments mixed up. once you change the arguments though, i believe you will have to change to if previous_balance < transaction_amount:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.