Sal's Shipping "NameError "ground" is not defined?

Hello I’m struggling with this code, can someone please let me know the solution here:

every time I run this code i get the

OUTPUT SCREEN & ERROR MESSAGE:

53.6
6.75
Traceback (most recent call last):
  File "script.py", line 37, in <module>
    if ground < premium and ground < drone:
NameError: name 'ground' is not defined

CODE FOR SAL SHIPPING PROJECT::

def ground_shipping(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(ground_shipping(8.4))

shipping_premium_cost = 125

def drone_shipping(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(drone_shipping(1.5))

def cheapest_shipping_method(weight):

  ground = ground_shipping(weight)

  premium = shipping_premium_cost

  drone = drone_shipping(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)def ground_shipping(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(ground_shipping(8.4))

shipping_premium_cost = 125

def drone_shipping(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(drone_shipping(1.5))


def cheapest_shipping_method(weight):

  ground = ground_shipping(weight)
  premium = shipping_premium_cost
  drone = drone_shipping(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)

Hi there, welcome to the forums. I edited your code so that it is correctly formatted.

Looking at it, you have an indentation error on the line that the Traceback is referencing; you’ve unindented your if statement out of the function scope, and there is no global ground variable so you get an error.

1 Like