Python 3: Sal's Shipping

Hi there,
I’m trying to complete Sal’s shipping assignment for Python 3, which instructs us to create 3 functions to: 1) determine the cost of ground shipping 2) determine the cost of drone shipping 3) determine the cheapest shipping method.

Everything in my program went well until I tried to call function 3, which i named Cheapest_cost(weight). this function worked normally when using an argument <= 10, but it gave the error below when I used an argument > 10…

Traceback (most recent call last):
File “”, line 45, in
File “”, line 41, in Cheapest_cost
% (cost, method)
UnboundLocalError: local variable ‘cost’ referenced before assignment

Anyone know what the problem is?

 if Ground_cost<=Drone_cost and Drone_cost<=Premium_cost:
   method = "ground shipping"
   cost = Ground_cost

How the other two relate to one another isn’t relevant to finding out whether ground cost is the cheapest

for example:

ground = 100
drone = 200
premium = 150

the cheapest one is ground, but drone is not <= premium so you won’t pick ground in this case


Others have already explained, but just to clarify: When weight = 12 as in your example where it is breaking, then:
-Ground_cost is $77
-Drone_cost is $177 and
-Premium cost is $125 (which you have set - but should probably do that within the function where you are using it)

The problem is that these numbers do not satisfy any of the conditions in your if/elif statements:
-Ground_cost<=Drone_cost and Drone_cost<=Premium_cost isn’t satisfied because of 177 is not less than 125
-Drone_cost<=Ground_cost and Ground_cost<=Premium_cost isn’t satisfied because 177 is not less than 77
-Premium_cost<=Drone_cost and Drone_cost<=Ground_cost isn’t satisfied because 177 is not less than 77

SO when you input weight = 12, none of the conditions are satisfied, so the variable “cost” never gets created within the function, and then you are trying to return it without assigning it.

1 Like

Ah, I see. I updated my code and it’s working fine now. Thanks for the advice everyone! :slightly_smiling_face:

 if Ground_cost<=Drone_cost and Ground_cost<=Premium_cost:
   method = "ground shipping"
   cost = Ground_cost
 elif Drone_cost<=Ground_cost and Drone_cost<=Premium_cost:
   method = "drone shipping"
   cost = Drone_cost
 elif Premium_cost<=Drone_cost and Premium_cost<=Ground_cost:
   method = "premium shipping"
   cost = Premium_cost

You’ve got six comparisons there.
Finding the smallest out of three only requires two comparisons:

start with the first.
if the second is smaller, replace
if the third is smaller, replace

also noteworthy is that if you have determined that ground is not smallest, then you don’t need to check whether ground is smaller than drone. it’s not. that test has already been carried out. same for premium - if you’ve determined that neither of the others is smaller, then premium is smallest. this ends up as three comparisons, so the previous strategy is still better with its two comparisons.

you can put the options in a list and iterate through it, making a replacement each time a smaller option is found

at that point you’ll have implemented the algorithm for min

(and if you’re implementing min then that suggests that you could use the already existing min instead, which, yes, you can)

1 Like