2016-10-08 6 views
-1

私はPythonプログラムを実行し、分類コードを要求する場所に "D"または "d"を入力すると、入力したかのように結果が吐き出されます " 「b」または「B」である。 elifのステートメントは完全に無視されているので、私はその理由を理解できません。また、b B Dまたはdではない分類コードを入力すると、最初のifブロックを通過します。私に手伝ってくれてありがとう! :)私のプログラムがelif文を認識するのに問題があります

customerName = input("Please enter your name: ") 
age = int(input("Please enter your age: ")) 
code = input("Please enter your classification code: ") 
numOfDays = int(input("Please enter the number of days you rented a vehicle: ")) 
initialOdometer = int(input("Please enter the initial odometer reading on the vehicle: ")) 
finalOdometer = int(input("Please enter the final odometer reading on the vehicle: ")) 

if code == "B" or "b" : 
    if age < 25: 
     kmDriven = finalOdometer - initialOdometer 
     kmDrivenCharge = kmDriven * 0.30 
     totalCost = 20.00 * numOfDays + (10 * numOfDays) 
     totalCost = totalCost + kmDrivenCharge 
    else: 
     kmDriven = finalOdometer - initialOdometer 
     kmDrivenCharge = kmDriven * 0.30 
     totalCost = 20.00 * numOfDays 
     totalCost = totalCost + kmDrivenCharge 
    print("\n") 
    print("SUMMARY:") 
    print("\n") 
    print("Customer's Name:",customerName) 
    print("Customer's Age:",age) 
    print("Customer's classification code:",code) 
    print("Number of days the vehicle was rented:",numOfDays) 
    print("The vehicle's odometer reading at the start of the rental period:",initialOdometer) 
    print("The vehicle's odometer reading at the end of the rental period:",finalOdometer) 
    print("The number of kilometers driven during the rental period:",kmDriven) 
    print("\n") 
    print("total Cost:","$","{0:.2f}".format(totalCost)) 

elif code == "D" or "d" : 
    if age < 25: 
     kmDriven = finalOdometer - initialOdometer 
     kmPerDay = kmDriven/numOfDays 
     if kmPerDay > 100: 
      kmDrivenCharge = ((kmPerDay - 100) * 0.30) * numOfDays 
     else: 
      kmDrivenCharge = 0 
     totalCost = numOfDays * 50.00 + (numOfDays * 10.00) 
     totalCost = totalCost + kmDrivenCharge 
    else: 
     kmDriven = finalOdometer - initialOdometer 
     kmPerDay = kmDriven/numOfDays 
     if kmPerDay > 100: 
      kmDrivenCharge = ((kmPerDay - 100) * 0.30) * numOfDays 
     else: 
      kmDrivenCharge = 0 
     totalCost = numOfDays * 50.00 
     totalCost = totalCost + kmDrivenCharge 
    print("\n") 
    print("SUMMARY:") 
    print("\n") 
    print("Customer's Name:",customerName) 
    print("Customer's Age:",age) 
    print("Customer's classification code:",code) 
    print("Number of days the vehicle was rented:",numOfDays) 
    print("The vehicle's odometer reading at the start of the rental period:",initialOdometer) 
    print("The vehicle's odometer reading at the end of the rental period:",finalOdometer) 
    print("The number of kilometers driven during the rental period:",kmDriven) 
    print("\n") 
    print("total Cost:","$","{0:.2f}".format(totalCost)) 
else : 
    print("\n") 
    print("ERROR: Invalid Classification Code") 
    print("\n") 
    print("Invalid Code:", code) 
    print("Customer Name:", customerName) 
    print("Customer Age:", age) 
+2

'code ==" D "またはcode ==" d "'またはより上品に: 'code.lower()==" d "' – Li357

+0

または 'code in 'bB''それは常に' True'に評価される最初の 'if'文の'または 'で、' elif'には決して到着しません。 – AChampion

答えて

0

問題はif文自体にあります。

if code == "B" or "b": 

"b"は常にtrueになります。 、これは有効なPythonで

if code == "B" or "b" : 

if "b": 
    print("hey this string wasn't empty") 

だから、あなたが書くために必要なものです::

if code == "B" or code == "b": 
+0

あなたの答えと@ LeviNoeckerの答えは一緒になります。 –

+0

すべての応答ありがとう!私は最初のifステートメントを変更し、今はうまくいきます。もう一度ありがとう、本当にありがとう! – cbatu

1

あなたの問題があれば、あなたの初期である は、例えば、このプログラムを作ってみますあなたの考え方を操作することはできません。 Pythonはorステートメントの真偽を評価しています。左側は正当な、正直な比較ですが、右側は実際の比較ではありません。むしろ、通訳者は文字列値bが真実か偽であるかどうかを評価しています。空でない文字列は真実とみなされるため、比較の右側は常にTrueと評価されるため、if文は常に実行されます。

関連する問題