Python 3: Sal's shipping project

https://www.codecademy.com/courses/learn-python-3/projects/sals-shipping
I’ve copied the code from the walkthrough video but surprising still getting a Nameerror:
This is the message from the terminal:
Traceback (most recent call last):
File “script.py”, line 41, in
if ground < premium and ground < drone:
NameError: name ‘ground’ is not defined

but ground is defined on this line:
ground = shipping_cost_ground(weight)

Any help or guidance would be appreciated.

Difficult to say without seeing the full code, please copy paste the full code to the forum

have you considered scope rules?

1 Like
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_of_drone(weight):
  
  if weight <= 2:
    price_per_pound = 4.50
  elif weight <= 6:
    price_per_pound = 9.00
  elif weight <= 10:
    price_per_pound = 12.00
  else:
    price_per_pound = 14.25
  
  return price_per_pound * weight

print(shipping_cost_of_drone(1.5))



def print_cheapest_shipping_method(weight):

  ground = shipping_cost_ground(weight)
  premium = shipping_cost_premium
  drone = shipping_cost_of_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)

Did you miss this:

scope rules are indeed the problem, you define local variable in the function, and then you try to access these variable outside the body of the function.

1 Like

Your indentation is screwed up. You if, elif, and else statements should be inside your function

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)


shipping_cost_premium = 125.00

def shipping_cost_of_drone(weight):
  
  if weight <= 2:
    price_per_pound = 4.50
  elif weight <= 6:
    price_per_pound = 9.00
  elif weight <= 10:
    price_per_pound = 12.00
  else:
    price_per_pound = 14.25
  
  return price_per_pound * weight


def print_cheapest_shipping_method(weight):
  ground = shipping_cost_ground(weight)
  premium = shipping_cost_premium
  drone = shipping_cost_of_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)

I’ve made them into local variables which works but the print statement at the bottom comes up with this error:
“Traceback (most recent call last):
File “script.py”, line 48, in
(cost, method)
NameError: name ‘cost’ is not defined”

also, i have indented it properly its just the format in which it pastes.

this answers your own question? The variable have a local scope, so these variable only exist within the body of the function. So trying to access these variable outside/after the function will give an error.

1 Like

Hi! Here is my solution. It is not as elegant as the one in the tutorial video, but I am still learning :slight_smile:

1 Like