我无法让我的程序识别elif语句

时间:2016-10-08 04:06:07

标签: python

所以,当我运行python程序并输入" D"或" d"在它要求分类代码的地方,它吐出结果,好像我输入了" b"或" B"。 elif声明被完全忽略了,我无法弄清楚原因。此外,当我输入的分类代码不是B D或d时,它仍会通过第一个if块进行。在此先感谢您的帮助! :)

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 个答案:

答案 0 :(得分:1)

您的问题出现在您的首字母:

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

这是有效的python,但不是你认为它的运作方式。 Python正在评估or语句双方的真实性。左侧是有效的,直接的比较,但右侧不是实际的比较。相反,解释器正在评估字符串值b是否真实或虚假。由于非空字符串被认为是真实的,因此比较的右侧总是计算为True,因此始终执行if语句。

答案 1 :(得分:0)

你的问题在于if语句本身。

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

“b”总是如此。 例如,尝试制作这个程序:

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

所以你需要写的是:

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