Sal's Shipping

Hello,
for the Sal’s Shipping exercise in Learn Python 3 I took additional steps to have the code print which shipping method was cheapest using:

weight = 41.5

#Ground Shipping
if weight <= 2:
cost_ground = weight * 1.50 + 20.00
elif weight > 2 and weight <= 6:
cost_ground = weight * 3 + 20.00
elif weight > 6 and weight <= 10:
cost_ground = weight * 4 + 20.00
else:
cost_ground = weight * 4.75 + 20.00
print(cost_ground)

#Ground Shipping Premium
cost_ground_premium = 125.00
print(cost_ground_premium)

#Drone Shipping
if weight <= 2:
drone_shipping = weight * 4.50
elif weight > 2 and weight <= 6:
drone_shipping = weight * 9
elif weight > 6 and weight <= 10:
drone_shipping = weight * 12
else:
drone_shipping = weight * 14.25
print(drone_shipping)

Cheapest_method = “”
if drone_shipping < cost_ground and cost_ground_premium:
Cheapest_method = drone_shipping
if cost_ground < drone_shipping and cost_ground_premium:
Cheapest_method = cost_ground
if cost_ground_premium < drone_shipping and cost_ground:
Cheapest_method = cost_ground_premium
print(Cheapest_method)

It works, however I assume there is a quicker way to create this function? I also want it to say. “The cheapest shipping method is X”. How can I go about doing this?

This documentation shows a few different print formatting syntaxes.
I like `print(f"{variable} is printed")
https://docs.python.org/3/tutorial/inputoutput.html

I don’t think this is working the way you expect. If you want multiple comparisons in an if statement, you need to write them out. This line is only asking if cost_ground_premium is True or False, not if it’s greater than drone_shipping.
The long way to rewrite that statement would be:

if drone_shipping < cost_ground:
  if cost_ground_premium:
    Cheapest_method = drone_shipping

One last thing, surround your code in backticks to keep indentation and formatting.

1 Like