3 Comparison Operators

https://www.codecademy.com/courses/learn-python-3/projects/sals-shipping

I am currently working through this project and I’m just wondering if I could get a little clarity on how to compare 3 variables.

premium_charge = 125.00

def cost_of_ground_ship (weight):
  flat_charge = 20.00
  if (weight > 10):
    charge = 4.75 * weight
  elif (weight > 6) and (weight <= 10):
    charge = 4.00 * weight
  elif (weight > 2) and (weight <= 6):
    charge = 3.00 * weight
  else:
    charge = 1.50 * weight
  return charge + flat_charge

#print(cost_of_ground_ship(8.4))

def cost_of_drone_ship(weight):
  if (weight > 10):
    charge = 14.25 * weight
  elif (weight > 6) and (weight <= 10):
    charge = 12.00 * weight
  elif (weight > 2) and (weight <= 6):
    charge = 9.00 * weight
  else:
    charge = 4.50 * weight
  return charge

# print(cost_of_drone_ship(1.5))

def cost_of_ship (weight):
  print(str(weight) + "Lbs")
  print("The cost of ground ship will be £" + str(cost_of_ground_ship(weight)) + ".")
  print("The cost of drone ship will be £" + str(cost_of_drone_ship(weight)) + ".")
  print("The premium charge will be £" + str(premium_charge))
  if(cost_of_ground_ship(weight) < (cost_of_drone_ship(weight)) < premium_charge):
    return "Ground Shipping will be cheaper."
  elif(cost_of_drone_ship(weight) < (cost_of_ground_ship(weight)) < premium_charge):
    return "Drone Shipping will be cheaper."
  elif (premium_charge < (cost_of_ground_ship(weight) < (cost_of_drone_ship(weight)))):
    return "Premium Shipping will be cheaper"

print(cost_of_ship(4.8))
print(cost_of_ship(41.5))

So i’m saying at the end, if the cost of the ground shipping is less than the drone shipping and less than premium shipping, then return the string. Which works with the first value because it is the cheapest, but when the cost of drone and ground shipping is more than the premium it should return the last string but it doesn’t it says:

4.8Lbs
The cost of ground ship will be £34.4.
The cost of drone ship will be £43.199999999999996.
The premium charge will be £125.0
Ground Shipping will be cheaper.
41.5Lbs
The cost of ground ship will be £217.125.
The cost of drone ship will be £591.375.
The premium charge will be £125.0
None

I’m wondering how I should be comparing 3 variables like this. Any help?

I am just updating this thread as I have figured out where I was going wrong.

Instead of comparing 3 operators use AND or OR to compare the trues and falses:

def cost_of_ship (weight):
  print(str(weight) + "Lbs")
  print("The cost of ground ship will be £" + str(cost_of_ground_ship(weight)) + ".")
  print("The cost of drone ship will be £" + str(cost_of_drone_ship(weight)) + ".")
  print("The premium charge will be £" + str(premium_charge))
  if(cost_of_ground_ship(weight) < cost_of_drone_ship(weight)) and (cost_of_ground_ship(weight) < premium_charge):
    return "Ground Shipping will be cheaper."
  elif(cost_of_drone_ship(weight) < cost_of_ground_ship(weight)) and (cost_of_drone_ship(weight) < premium_charge):
    return "Drone Shipping will be cheaper."
  elif (premium_charge < cost_of_ground_ship(weight)) and (premium_charge < cost_of_drone_ship(weight)):
    return "Premium Shipping will be cheaper."

print(cost_of_ship(4.8))
print(cost_of_ship(41.5))

Yep. You would use and .
good catch.

1 Like

You’re making a lot of comparisons to find the smallest of 3 values. Consider the logic here:

a = 5
b = 6
c = 2

def find_smallest(n1, n2, n3):
    smallest = n1
    if n2 < n1:
        smallest = n2
    if n3 < n2:
        smallest = n3
    return smallest

print(find_smallest(a, b, c)) #2
print(find_smallest(b, c, a)) #2
print(find_smallest(c, a, b)) #2
1 Like