Python Control Flow Sal's Shipping

Hi People,

just a quick one (hopefully). I keep getting this error message when trying to run the function and print. I’m following the video tutorial and seem to keep getting it. Anyone see why its happening from my code?

Thank you

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

premium_ground_ship = 125.00
def cost_of_ground(weight):
if weight <= 2:
return (1.50weight + 20)
elif weight <=6:
return (3.00
weight + 20)
elif weight <=10:
return (4.00weight + 20)
else:
return (4.75
weight + 20)

print(cost_of_ground(8.4))

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

print(cost_of_drone(1.5))

def print_cheapest_method(weight):

ground = cost_of_ground(weight)
drone = cost_of_drone(weight)
premium = premium_ground_ship

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

print(
"The cheapest method cost is $%.2f and it is %s delivery "
% (cost, method)
)

print_cheapest_method(4.8)
print_cheapest_method(41.5)

Have a look at this FAQ for submitting code to posts which would preserve your indentation and such.

Where is cost defined? The error suggests there’s no identifiers known as cost within the given scope.

premium_ground_ship = 125.00
def cost_of_ground(weight):
  if weight <= 2:
    return (1.50*weight + 20)
  elif weight <=6:
    return (3.00*weight + 20)
  elif weight <=10: 
    return (4.00*weight + 20)
  else:
    return (4.75*weight + 20)

print(cost_of_ground(8.4))

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

print(cost_of_drone(1.5))

def print_cheapest_method(weight):
  
  ground = cost_of_ground(weight)
  drone = cost_of_drone(weight)
  premium = premium_ground_ship
  
  if drone< ground and drone< premium:
    method = "standard drone"
    cost = drone
  elif ground< premium and ground< drone:
    method = "standard ground"
    cost = ground
  else:
    method = "premium ground"
    cost = premium
    
print(
  "The cheapest method cost is $%.2f and it is %s delivery " 
  % (cost, method)
)

print_cheapest_method(4.8)
print_cheapest_method(41.5)



does that do the trick?

cost is defined within the print_cheapest_method function, yet you are trying to access the variable from a global scope. That means you’re trying to access a variable that is specific to the print_cheapest_method function outside of the function. Here is an article that explains it well.
I hope this helps!

2 Likes

thank you for the reply! I just figured out it was due to the indentation of the final print function at the end and the following line

1 Like

just realised that was what you were saying also!

1 Like