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()
你在問什麼?你的代碼有什麼問題? – Nicarus
您需要在Transaction_Code輸入行後面的main()中放入適當的'if'語句。 – jcfollower
此外,讓您的生活更輕鬆,並將您的輸入強制轉換爲單個外殼(使用lower或upper)以將輸入更改爲大寫或小寫,並與單個外殼進行比較,那麼您所要做的就是'Transaction_Code.lower()==「w」'。或者只是'Transaction_code'('w','W')' – idjaw