Sal's Shipping

Good Morning, as I was going through the Sal’s Shipping exercise, my almost finished code looked like this:

def standard_ground_shipping(weight):
  if (weight <= 2):
    price = weight * 1.5
  elif (weight <= 6) and (weight > 2):
    price = weight * 3
  elif (weight <= 10) and (weight > 6):
    price = weight * 4
  else:
    price = weight * 4.75 
  return price + 20

def drone_shipping(weight):
  if (weight <= 2):
    price = weight * 4.5
  elif (weight <= 6) and (weight > 2):
    price = weight * 9
  elif (weight <= 10) and (weight > 6):
    price = weight * 12
  else:
    price = weight * 14.25
  return price

def lowest_shipping_price(weight):
  drone = drone_shipping(weight)
  standard = standard_ground_shipping(weight)
  premium = 125
  if drone < standard:
    return(f"The cheapest shipping method is drone shipping. It will cost you {drone} dollars.")
  if standard < drone:
    return(f"The cheapest shipping method is standard shipping. It will cost you {standard} dollars.")
  elif premium < standard and premium < drone:
    return(f"The cheapest shipping method is premium shipping. It will cost you {premium} dollars.")

When i began to test the code with a value that would result in premium shipping being the cheapest, it would say that drone shipping would be cheaper even though the price with drone shipping would be over 125. I began to move things around and made the following change:

def lowest_shipping_price(weight):
  drone = drone_shipping(weight)
  standard = standard_ground_shipping(weight)
  premium = 125
  if premium < standard and premium < drone:
    return(f"The cheapest shipping method is premium shipping. It will cost you {premium} dollars.")
  if drone < standard:
    return(f"The cheapest shipping method is drone shipping. It will cost you {drone} dollars.")
  elif standard < drone:
    return(f"The cheapest shipping method is standard shipping. It will cost you {standard} dollars.")

(I moved the part that checks if premium is cheaper than standard and drone shipping to the top)
Then it began to work properly. This puzzles me, what is the reason why moving that part to the top would fix the issue I was having?

Where did you have the check before then? Last? Like so:

  if drone < standard:
    return(f"The cheapest shipping method is drone shipping. It will cost you {drone} dollars.")
  elif standard < drone:
    return(f"The cheapest shipping method is standard shipping. It will cost you {standard} dollars.")
  elif premium < standard and premium < drone:
    return(f"The cheapest shipping method is premium shipping. It will cost you {premium} dollars.")

now standard and drone shipping are compared first, resulting in one of them to be true (unless the cost are equal), so we never consider the premium shipping option.

1 Like

Gotcha, thank you for helping me understand! I’m still pretty new at this and I appreciate all the help!

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.