2

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!

discindude
  • 11
  • 2
  • Sorry but I'm pretty new to this and the link you provided doesn't seem to really clear it up for me. Thanks for the quick response though. – discindude Sep 30 '15 at 15:57
  • It should be `if trans_code == "W" or trans_code == "w":` The way you had it, `if trans_code == "W" or "w":`, was being evaluated like so: Python checks if `trans_code == "W"` is `True`. If it isn't, Python checks if `"w"` is `True`. Since a non-empty string always evaluates as `True`, your full `if` condition was always evaluating as `True` and Python never went to the `elif`. Now, the call to the `deposit` function will fail because `new_bal` isn't defined. I think you want to pass `trans_amt`, not `new_bal`, to it. Also, change `new_bal` to `trans_amt` in your `deposit` declaration params. – Jason Sep 30 '15 at 16:38
  • Thanks Jason! Your answers and explanations were spot on. This had me stumped. Not only did your answers solve the problem, but your explanation of why it did not work gave me a greater understanding of what I did wrong. Thanks again! – discindude Sep 30 '15 at 19:32

0 Answers0