2016-10-10 93 views
0

Transaction_Code == "W""w""D""d"的Python ELIF爲了不工作,我想

如果不是,它應該運行Process_Invalid_Code(Previous_Balance)

正在發生的事情,但是如果是輸入Transaction_Code!= "W""w""D""d"後,它將繼續運行"What is your previous balance?""How much is the transaction amount?"輸入...

只有到那時,你給那些輸入它運行Invalid_Transaction_Code

後,我希望發生的是,它拋出Invalid_Transaction_Code ("Invalid Transaction Code!")前等不浪費用戶的時間要求上餘額和交易..

不有道理?

下面是代碼

#The main function definition 

def main(): 
    Name = input("What is your name? ") 
    Account_ID = input("What is your account ID? ") 
    Transaction_Code = input("Press W or w for Withdrawal, Press D or d for Deposit: ") 
    Previous_Balance = float(input("What is your previous balance? ")) 
    Transaction_Amount = float(input("How much is the transaction amount? ")) 

    if Transaction_Code == "W" or Transaction_Code == "w": 
     Withdrawal_Process(Previous_Balance, Transaction_Amount) 
    elif Transaction_Code == "D" or Transaction_Code == "d": 
     Deposit_Process(Previous_Balance, Transaction_Amount) 
    else: 
     Process_Invalid_Code(Previous_Balance) 

#Defines the Deposit Process 

def Deposit_Process(Previous_Balance, Transaction_Amount): 
    New_Balance = Transaction_Amount + Previous_Balance 
    Print_Function(New_Balance) 


#Defines the Withdrawal Process 

def Withdrawal_Process(Previous_Balance, Transaction_Amount): 
    if Transaction_Amount > Previous_Balance: 
     print("Invalid Transaction: Not Sufficient Funds!") 
     New_Balance = Previous_Balance 
     Print_Function(New_Balance) 
    else: 
     New_Balance = Previous_Balance - Transaction_Amount 
     Print_Function(New_Balance) 

#The Invalid Code Function Definition 

def Process_Invalid_Code(Previous_Balance): 
    New_Balance = Previous_Balance 
    print ("Invalid Transaction Code!") 
    print ("Please type W or w for Withdrawal") 
    print ("or type D or d for Deposit") 
    Print_Function(New_Balance) 


#Defines the Print Function 

def Print_Function(New_Balance): 
    print ("Your balance is now $", format(New_Balance, '.2f')) 


#Call the main function 

main() 
+1

你在問什麼?你的代碼有什麼問題? – Nicarus

+0

您需要在Transaction_Code輸入行後面的main()中放入適當的'if'語句。 – jcfollower

+1

此外,讓您的生活更輕鬆,並將您的輸入強制轉換爲單個外殼(使用lower或upper)以將輸入更改爲大寫或小寫,並與單個外殼進行比較,那麼您所要做的就是'Transaction_Code.lower()==「w」'。或者只是'Transaction_code'('w','W')' – idjaw

回答

4

因爲所有你想要的操作需要Previous_Balance你必須在任何情況下提出要求:

def main(): 
    # never used, lets ask anyway 
    Name = input("What is your name? ") 

    # we need this information at a minimum 
    Previous_Balance = float(input("What is your previous balance? ")) 
    Transaction_Code = input("Press W or w for Withdrawal, Press D or d for Deposit: ") 

    # if its a withdrawal/deposit, find the amount and account 
    if Transaction_Code.upper() in "WD": 
     # we never use this Account_ID ... 
     Account_ID = input("What is your account ID? ") 
     Transaction_Amount = float(input("How much is the transaction amount? ")) 
     if Transaction_Code.upper() == "W": 
      Withdrawal_Process(Previous_Balance, Transaction_Amount) 
     else: 
      Deposit_Process(Previous_Balance, Transaction_Amount) 
    else: 
     # they've entered a bad code 
     Process_Invalid_Code(Previous_Balance) 
+0

謝謝,這是我正在尋找的。 – Alex

+1

@Alex那麼你應該接受答案:-) – donkopotamus

+0

這是一個編程課程介紹的作業......我將我的代碼從提供的僞代碼中除去。因此,爲什麼我敢肯定很多這是不必要的/更慢的方式做事 – Alex

1

您可能也想嘗試使用的raw_input()反對在必要時輸入()或使用variable_name投射輸出以擺脫tracback錯誤。