Sal's Shipping Project Error

Hey Guys!

I have been having a name error show up (in relation to Sal’s Shipping Python 3 project) after I save my code after completing all the tasks. I’ve read the code over again a couple of times, but no luck so far. I am also new to coding and Python 3, so still trying to learn how it operates. So, I was wondering if anyone else could help me figure out what the issues are. Thank you in advance!

My code:

def shipping_cost_ground(weight):

  if weight <= 2: 
    price_per_pound = 1.50
  
  elif weight <= 6:
    price_per_pound = 3.00
  
  elif weight <= 10:
    price_per_pound = 4.00
  
  else:
    price_per_pound = 4.75

  return 20 + (price_per_pound + weight)

  print(shipping_cost_ground(8.4))


  shipping_cost_premium = 125.00


def shipping_cost_price(weight):

  if weight <= 2: 
    price_per_pound = 1.50
  
  elif weight <= 6: 
    price_per_pound = 3.00
  
  elif weight <= 10:
    price_per_pound = 4.00
  
  else:
    price_per_pound = 4.75

  return price_per_pound * weight

  print(shipping_cost_drone(1.5))

def print_cheapest_shipping_method(weight):

  ground = shipping_cost_ground(weight)
  premium = shipping_cost_premium
  drone = shipping_cost_drone(weight)

  if ground < premium and ground < drone:
    method = "standard ground"
    cost = ground
  elif premium < ground and premium < drone:
    method = "premium ground"
    cost = premium
  else:
    method = "drone"
    cost = drone

  print(
    "The cheapest option available is $%.2f with %s shipping."
  % (cost, method))

print_cheapest_shipping_method(4.8)
print_cheapest_shipping_method(41.5)

This error is shown:

Traceback (most recent call last):
File “shipping.py”, line 62, in
print_cheapest_shipping_method(4.8)
File “shipping.py”, line 44, in print_cheapest_shipping_method
premium = shipping_cost_premium
NameError: name ‘shipping_cost_premium’ is not defined

Where is shipping_cost_premium defined? Is it a local or global variable? This will give a hint as to the particular error, “NameError: name ‘shipping_cost_premium’ is not defined”.

Hint, this has to do with where the variable is defined and how you’re trying to access that variable. (This is a scope issue).

So, how would you fix this? Take a shot at it. Move the code around and see what happens.
If you haven’t already done so, poke around here on the forums for this particular question about this project. This question has come up often.

1 Like

Okay, will give it a try by re-arranging some of the code. Thank you for the help and advice!

1 Like