Sal's shipping

Hi!
My code is really different from the one on the walkthrough video.
It works this way. But the way Matt explained cheapest method got me confused, especially with $%.2f and % (cost, method). It was never explained in the course.

Hello @faridsaryev421170057, welcome to the forums! The % operator in this case is a type of string formatting operator (not to be confused with the modulo operator-which looks the same (%), but is used for a different purpose). I would recommend reading this article on how it is used.

Your code for cheapest shipping method is wrong by the way.
If drone and ground has the same price you’ll end up saying premium is cheapest.

def cheapest_option():
  ground = 3
  premium = 1000
  drone = 3
  if ground < drone and ground < premium:
    print("Ground shipping is the better option for you! Your total cost is " + str(ground))
  elif drone < ground and drone < premium:
    print("Drone shipping is the better option for you! Your total cost is " + str(drone))
  else:
    print("Premuim ground shipping is the better option for you! Your total cost is " + str(premium))

cheapest_option()

This does happen at weight 3 + 1 / 3 (blue and orange lines crossing)

Figure_1

import matplotlib.pyplot as plot

xs = [n / 10 for n in range(350)]
plot.plot(xs, [drone_shipping_price(x) for x in xs], label="drone")
plot.plot(xs, [ground_shipping_price(x) for x in xs], label="ground")
plot.plot(xs, [125 for x in xs], label="premium")
plot.legend()
plot.show()
weight = 3 + 1 / 3
print('drone price:', drone_shipping_price(weight))  # 30.0
print('ground price:', ground_shipping_price(weight))  # 30.0
cheapest_option(weight)  # 125.0 ... oops.

Thank you for your response!
Your link is really useful.

1 Like

Thank you for your response!
But in this exercise is always given exact weight. Who would give 3+1/3 weight and for what?
And in that case what the difference between my “cheapest function” vs Matt’s “cheapest function”?
I didn’t understand anything from the last code you send.
P.S I have zero IT background.

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