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

def cost_ground_shipping(weight):
  if weight <= 2:
    cost = 1.50*weight + 20
  elif weight <=6:
    cost = 3*weight + 20
  elif weight <= 10:
    cost = 4*weight + 20
  else:
    cost = 4.75*weight + 20
  return cost
#print (ground_shipping(8.4))
 
  premium_ground_shipping = 125
  
def drone_shipping(weight):
    if weight <= 2:
     cost = 4.50*weight
    elif weight <=6:
     cost = 9*weight
    elif weight <= 10:
     cost = 12*weight
    else:
     cost = 14.25*weight
    return cost
#print(drone_shipping(1.50))

def cheapest_delivery_method(weight):
 drone = drone_shipping(weight)
 premium = premium_ground_shipping
 ground = cost_ground_shipping(weight)

In here,
why can’t I put -

" drone = drone_shipping(weight)
 premium = premium_ground_shipping
 ground = cost_ground_shipping(weight)"

outside of the function cheapest_delivery_method?

Isn’t it like that if I put it outside, it would be accessible by all but here only accessible by the above function?

It’s showing NameError when i do so but why?

function parameters (like weight) have a local scope, so weight variable/parameter is only accessible in the body of the function.

sure, you could do:

drone = drone_shipping(5)
premium = premium_ground_shipping
ground = cost_ground_shipping(5)

but now we have duplicate code (specified the weight (5) twice). Which is why we wrap this logic in a function

furthermore, we are going to expand the functionality of the function in the next step