mercredi 30 septembre 2015

Python if elif else problems [duplicate]

This question already has an answer here:

New to python. Not sure what I'm doing wrong here. For some reason the if elif else statements are not working, and it just defaults to the first if statement no matter what the input. Kinda stuck here. According to everything I have read my syntax is correct. any help would be appreciated. Heres the code:

#main function
def main():
    name=input("Please input your name:")
    acct_id=input("Please input your account id:")
    trans_code=input("Please input your transaction code \
(w=withdrawal, d=deposit):")
    prev_bal=float(input("Please input your previous balance:"))
    trans_amt=float(input("Please input your transaction amount:"))

    if trans_code == "W" or "w":
        withdrawal(name,acct_id,trans_code,prev_bal,trans_amt)
    elif trans_code == "D" or "d":
        deposit(name,acct_id,trans_code,prev_bal,new_bal)
    else:
        print("This transaction is invalid")

#withdrawal function        
def withdrawal(name,acct_id,trans_code,prev_bal,trans_amt):
    if trans_amt > prev_bal:
        print("Not enough funds")
    else:
        new_bal=prev_bal-trans_amt
        print_bal(name,acct_id,new_bal)
#deposit function
def deposit(name,acct_id,trans_code,prev_bal,new_bal):
    new_bal=prev_bal+trans_amt
    print_bal(name,acct_id,new_bal)

#print function
def print_bal(name,acct_id,new_bal):
    print("Hello:",name)
    print("Your account ID is:",acct_id)
    print("Your new balance is:$",format(new_bal,",.2f"))
main()

Thanks in advance for any input!

Aucun commentaire:

Enregistrer un commentaire