Sal's Shipping Error help

Hi all,

I’m going through Python 3 and am stuck at Sal’s Shipping Challenge.

(https://www.codecademy.com/courses/learn-python-3/projects/sals-shipping?action=resume_content_item)

I’ve read pages of different issues but nothing I can see in my code is incorrect (clearly there is though).

The error message I’m receiving is

Traceback (most recent call last):
File “script.py”, line 65, in
%(cost, method)
NameError: name ‘cost’ is not defined

I hope this code comes through correctly. My code is as follows:

I have returned a cost for Q1 and Q4 and questions 1-5 are all fine. Q6 is where the error is occurring. Thanks in advance

# Q1 function for Ground Shipping

def ground_shipping(weight):
  
  if weight <= 2:
    price = weight * 1.5
  elif weight <= 6.0:
    price = weight * 3
  elif weight <= 10.0:
    price = weight * 4
  else:
    price = weight * 4.75
  cost = price + 20
  return cost

# Q3 variable for Premium Shipping

premium_shipping = 125


# Q4 function for Ground Shipping

def drone_shipping(weight):
  if weight <= 2:
    price = weight * 4.5
  elif weight <= 6.0:
    price = weight * 9
  elif weight <= 10.0:
    price = weight * 12
  else:
    price = weight * 14.25
  cost = price
  return cost

# Q6 Cheapest Shipping method and associated cost 

def cheapest_shipping_method(weight):
 
  ground = ground_shipping(weight)
  premium = premium_shipping
  drone = drone_shipping(weight)
  
  if ground < premium and ground < drone:
    method = 'standard ground shipping'
    cost = ground
  elif premium < ground and premium < drone:
    method = 'premium ground shipping'
    cost = premium
  else:
    method = 'drone shipping'
    cost = drone

# Q2 - print with 8.4lbs

print(ground_shipping(8.4))

# Q5 - print with 1.5lbs

print(drone_shipping(1.5))


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

cost is defined within the body of the cheapest_shipping_method function, thus it has a local scope (the variable only exists within body of the function), so trying to access cost outside the function results in an error

4 Likes

@stetim94, thanks, lesson learned

This topic was automatically closed after 23 hours. New replies are no longer allowed.