# 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 “script.py”, line 45, in
Cheapest_cost(12)
File “script.py”, line 41, in Cheapest_cost
% (cost, method)
UnboundLocalError: local variable ‘cost’ referenced before assignment

Anyone know what the problem is?

``````                                vvvvvvvvvvvvvvvvvvvvvvvv
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
``````

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

2 Likes

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!

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

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