Chris
Chris

Reputation: 23

python adding a negative sign to calculation result

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()

Upvotes: 2

Views: 247

Answers (3)

Rob Taylor
Rob Taylor

Reputation: 604

You are passing the arguments backwards.

process_withdrawal (transaction_amount, previous_balance)

and

def process_withdrawal (previous_balance, transaction_amount):

Upvotes: 1

Alpha Codemonkey
Alpha Codemonkey

Reputation: 3261

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)

Upvotes: 3

moesef
moesef

Reputation: 4851

I think your if statement within process withdrawl should be

if previous_balance < transaction_amount:

Upvotes: 0

Related Questions