-1

So when I run the python program and I input "D" or "d" where it asks for the classification code, it spits out the results as if I inputted "b" or "B". The elif statement is being completely ignored and I can't figure out why. Also, when I input a classification code that isn't b B D or d, it still goes through with the first if block. Thanks in advance for helping me! :)

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)
cbatu
  • 11
  • 3

2 Answers2

1

Your problem is in your initial if:

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

This is valid python, but not operating how you think it is. Python is evaluating the truthiness of both sides of the or statement. The left side is a valid, straight forward comparison, but the right side isn't an actual comparison. Rather, the interpreter is evaluating whether or not the string value b is truthy or falsy. Since non empty strings are considered truthy, the right side of the comparison always evaluates to True, so the if statement is always executed.

Levi Noecker
  • 3,142
  • 1
  • 15
  • 30
0

Your issue lies in the if statements themselves.

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

"b" is always going to be true. For example try making this program:

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

So what you need to write is:

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