Python如果有其他问题

时间:2015-09-30 15:48:35

标签: python if-statement

python新手。不知道我在这里做错了什么。由于某种原因,if elif else语句不起作用,并且无论输入是什么,它都默认为第一个if语句。有点卡在这里。根据我读过的一切,我的语法是正确的。任何帮助,将不胜感激。下面是代码:

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

提前感谢任何输入!

0 个答案:

没有答案