Another person needing help with Sal's Shipping

Hello all. For Sal’s Shipping, I am sure I have missed something simple, but I cannot decipher why this will not work. I watched the walk through and I get how that solution works and it is more efficient than how I did it, but I would like to know what I did wrong. Thanks!

https://www.codecademy.com/courses/learn-python-3/projects/sals-shipping

Code then output posted below:

# ground based on weight
def ground(weight):
  if weight <= 2:
     return weight * 1.50 + 20.00
  elif weight > 2 and weight <= 6:
    return weight * 3.00 + 20.00
  elif weight > 6 and weight <= 10:
    return weight * 4.00 + 20.00
  else:
    return weight * 4.75 + 20.00

# premium
premium = 125.00

# drone based on weight
def drone(weight):
  if weight <= 2:
     return weight * 4.50
  elif weight > 2 and weight <= 6:
    return weight * 9.00
  elif weight > 6 and weight <= 10:
    return weight * 12.00
  else:
    return weight * 14.25

# what is cheapest
def best(weight):
  
  ground = ground(weight)
  drone = drone(weight)
  prem = premium
  
  if ground < drone and ground < prem:
    print("The cheapest way to ship a " + str(weight) + "pound package is ground and it will be $" + ground + ".")
  elif drone < ground and drone < prem:
    print("The cheapest way to ship a " + str(weight) + "pound package is by drone and it will be $" + drone + ".")
  else:
    print("The cheapest way to ship a " + str(weight) + "pound package is with premium ground and it will be $125.00.")


best(4.8)
Traceback (most recent call last):
  File "script.py", line 41, in <module>
    best(4.8)
  File "script.py", line 29, in best
    ground = ground(weight)
UnboundLocalError: local variable 'ground' referenced before assignment

If you have several variables named ground, then which do you get when you read from it? Read your error message.

That is what I thought the message meant, but where am I using it multiple times? I thought the first time I was using it was on that line in the final function where it said

ground = ground(weight)

Is it that in using it in the function name earlier, I should not use it as a local variable in the last function? Or maybe I am just missing it, I have been staring at this a while…

Do whatever you want, just don’t be surprised if you don’t get two different things when you use the same name twice. Because that’s what you’re trying to do isn’t it, to sometimes have it mean a function and sometimes mean your local variable.

I see, yeah I will change that. I did not realize, since one was a variable and one was a function, that they would conflict.

1 Like

What you might legitimately find confusing though, is that at that time you have not yet executed code which creates the local variable.

Turns out function variables are not entirely dynamic, rather they’re all created at once for each function scope. This is so that they do not need to be searched for when used, which would cost extra time.

I see. I made a few changes and now it is working. I thought this must be the conflict but it was not specified in any lesson so wanted to really know if that could be the issue from someone more experienced. Thank you for the help!

2 Likes