# Sal’s Shipping with Functions

First post.
Saw the part about coming back “after having been introduced to functions”
Assumed that functions were somehow required.
Finished project realizing no functions were needed at all.
Decided to try incorporating functions into the solution.

This is the result I came up with:

``````# Set shipping weight
weight = 41.5

# Ground Shipping cost calculator
def calc_GS_cost(weight):
if weight <= 2:
GS_cost = weight * 1.5 + 20
elif weight <= 6:
GS_cost = weight * 3 + 20
elif weight <= 10:
GS_cost = weight * 4 + 20
else:
GS_cost = weight * 4.75 + 20
return GS_cost

GSP_cost = 125

# Drone Shipping cost calculator
def calc_DS_cost(weight):
if weight <= 2:
DS_cost = weight * 4.5
elif weight <= 6:
DS_cost = weight * 9
elif weight <= 10:
DS_cost = weight * 12
else:
DS_cost = weight * 14.25
return DS_cost

# Lowest shipping cost function
def lowest_ship_cost(weight):
GS_cost = calc_GS_cost(weight)
DS_cost = calc_DS_cost(weight)
if GS_cost < GSP_cost and GS_cost < DS_cost:
print("Ground Shipping is cheapest at", GS_cost, "dollars for", weight, "lbs.")
elif GSP_cost < GS_cost and GSP_cost < DS_cost:
print("Ground Shipping Premium is cheapest at", GSP_cost, "dollars for", weight, "lbs.")
elif DS_cost < GSP_cost and DS_cost < GS_cost:
print("Drone Shipping is cheapest at", DS_cost, "dollars for", weight, "lbs.")
else:
print("Undetermined")

lowest_ship_cost(weight)

``````

Haven’t managed to find a proper solution to handle instances where different shipping methods costs are the same.

What do you mean by

Haven’t managed to find a proper solution to handle instances where different shipping methods costs are the same.

Also note the DRY principle (don’t repeat yourself) in factoring can be applied here.
If you know something is going to repeat, you can anticipate it, example:

``````def calc_GS_cost(weight):
if weight <= 2:
GS_cost += weight * 1.5
elif weight <= 6:
GS_cost += weight * 3
elif weight <= 10:
GS_cost += weight * 4
else:
GS_cost += weight * 4.75
return GS_cost
``````

But even better

``````def calc_GS_cost(weight):
if weight <= 2:
cost_multiplier = 1.5
elif weight <= 6:
cost_multiplier = 3
elif weight <= 10:
cost_multiplier = 4
else:
cost_multiplier = 4.75
return weight * cost_multiplier + flat_fee
``````

This way you save yourself 6 operations from your original (the multiplications and additions). In terms of this program it doesn’t matter, especially as addition and multiplication are rather fast for CPUs to process, but this factoring pattern is useful when the type of operation you do might not be as efficient (particularly when scaled to a larger size).

3 Likes

Thanks for your advice. Wasn’t expecting such a thoughtful response so quickly.
I really admire your solution. It’s simple and intuitive; the way of coding that I’m trying to improve on.

The project was designed so that it’s very hard to input a weight that would result in more than 1 shipping option with the same shipping cost. (E.g. when weight is 3 and 1/3 lbs) I was just overthinking how to handle such an occurrence.

But, If I had to add extra code to handle this occurrence, I would just add extra if statements to the last function like so:

``````# Lowest shipping cost function
def lowest_ship_cost(weight):
GS_cost = calc_GS_cost(weight)
DS_cost = calc_DS_cost(weight)
if GS_cost == GSP_cost and GS_cost < DS_cost:
print("Both Ground Shipping and Ground Shipping Premium is cheapest at", GS_cost, "dollars for", weight, "lbs.")
elif GS_cost == DS_cost and GS_cost < GSP_cost:
print("Both Ground Shipping and Drone Shipping is cheapest at", GS_cost, "dollars for", weight, "lbs.")
elif DS_cost == GSP_cost and DS_cost < GS_cost:
print("Both Ground Shipping Premium and Drone Shipping is cheapest at", DS_cost, "dollars for", weight, "lbs.")
elif GS_cost < GSP_cost and GS_cost < DS_cost:
print("Ground Shipping is cheapest at", GS_cost, "dollars for", weight, "lbs.")
elif GSP_cost < GS_cost and GSP_cost < DS_cost:
print("Ground Shipping Premium is cheapest at", GSP_cost, "dollars for", weight, "lbs.")
elif DS_cost < GSP_cost and DS_cost < GS_cost:
print("Drone Shipping is cheapest at", DS_cost, "dollars for", weight, "lbs.")
else:
print("Undetermined")

``````

But this looks quite clunky and the else statement should probably be removed as it serves no purpose anymore.