mercredi 5 octobre 2016

Python 3 If else statement

I'm writing a program that processes potential bank transactions for deposits and withdrawals, given the starting balance and transaction amount.
I can get the program to run fine with just the coding for withdrawals or deposits, but when I code in both, it doesn't flow how I want.
For some reason it will always display my 'wrong transaction code' at the end. I've been fiddling around with the if's and else's, but still can't get it to work.
Please help. Thanks in advance.

Here's what I have:

name     = input('Enter name:               ')
ID       = input('Enter account ID:         ')
t_code   = input('Enter transaction code:   ')
previous = float(input('Enter balance:            '))
t_amount = float(input('Enter transaction amount: '))


if t_code == 'W' or t_code == 'w':
    if previous < t_amount:
        print('\n***Overdraft Error: not enough funds to process')
        print('\t\t    transaction.***')
    if previous > t_amount:
        withdrawal = previous - t_amount
        print('\n\tTransaction successfully processed.')
        print('\tYour new balance is: $', format(withdrawal, ',.2f'), sep='')
        print()
else:
    print('\n***Error: not a valid transaction code.')
    print('\t  please try again.***')

# why does it always show the 'wrong transaction code' message below?

if t_code == 'D' or t_code == 'd':
        deposit = previous + t_amount
        print('\n\tTransaction successfully processed.')
        print('\tYour new balance is', deposit)

else:
    print('\n***Error: not a valid transaction code.')
    input('\t  please try again.***')

Aucun commentaire:

Enregistrer un commentaire