Can't get Sal's Shipping to work

Hey everyone! I’ve been fiddling with Sal’s Shipping project for a few hours and can’t seem to get it to work. Can anyone help me figure out what’s wrong with my code?

This is what I originally had:


prem_ground = 125.00

def ground(weight):
  if weight > 10.0:
    return (weight * 4.75) + 20.0
  elif weight > 6.0:
    return (weight * 4.00) + 20.0
  elif weight > 2.0:
    return (weight * 3.00) + 20.0
  else:
    return (weight * 1.50) + 20.0
  return ground(weight)

def drone(weight):
  if weight > 10.0:
    return weight * 14.25
  elif weight > 6.0:
    return weight * 12.00
  elif weight > 2.0:
    return weight * 9.00
  else:
    return weight * 4.50
  return drone(weight)

def cheapest_shipping(weight):
  if drone(weight) <= ground(weight) and drone(weight) <= prem_ground:
    return "You should use Drone shipping, it will cost " + str(drone(weight))
  elif prem_ground(weight) < ground(weight) and prem_ground(weight) < drone(weight):
    return "You should use Premium Ground shipping, it will cost " + str(prem_ground)
  elif ground(weight) < prem_ground(weight) and ground(weight) < drone(weight):
    return "You should use Ground shipping, it will cost " + str(ground(weight))

cheapest_shipping(17)







This is what I’m trying now, with no luck:

prem_ground = 125.00

def ground(weight):

  if weight > 10.0:

    ppp = 4.75

  elif weight > 6.0:

    ppp = 4.00

  elif weight > 2.0:

    ppp = 3.00

  else:

    ppp = 1.50

  return (ppp * weight) + 20

def drone(weight):

  if weight > 10.0:

    ppp = 14.25

  elif weight > 6.0:

    ppp = 12.00

  elif weight > 2.0:

    ppp = 9.00

  else:

    ppp = 4.50

  return (ppp * weight)

def cheapest_shipping(weight):

  ground = ground(weight)

  drone = drone(weight)

  prem = prem_ground

  if drone < ground and drone <= prem_ground:

    method = "Drone"

    cost = drone

  elif prem_ground < ground and prem_ground < drone:

    method = "Premium Ground"

    cost = prem_ground

  elif ground < prem_ground and ground < drone:

    method = "Ground"

    cost = ground

  return "You should ship using " + method + " it will cost " + cost

cheapest_shipping(17)



The second one just loads forever. The first one gives errors I don’t understand. Help??

By the way, it has indents, they just don’t paste into the message for some reason.

why change from the first to the second approach?

I do get an error with your second code:

Traceback (most recent call last):
  File "main.py", line 71, in <module>
    cheapest_shipping(17)
  File "main.py", line 45, in cheapest_shipping
    ground = ground(weight)
UnboundLocalError: local variable 'ground' referenced before assignment

The problem seems to be that local variable and the function you try to call have the same name.

Thanks! After I restarted the window, I was able to get the error code and then fixed it. I changed my approach because the first was returning error codes that I couldn’t seem to fix. Do you know how I could have fixed the first approach?

the error message is pretty helpful:

Traceback (most recent call last):
  File "main.py", line 33, in <module>
    cheapest_shipping(17)
  File "main.py", line 28, in cheapest_shipping
    elif prem_ground(weight) < ground(weight) and prem_ground(weight) < drone(weight):
TypeError: 'float' object is not callable

looking at line 28:

elif prem_ground(weight) < ground(weight) and prem_ground(weight) < drone(weight):

prem_ground is a float, you can’t call a float like you call a function. The premium shipping doesn’t depends on weight.

That makes sense. In that case, how would I add in prem?

Maybe what I did in the other code would have worked- linking it into the function with prem = prem_ground.

you use prem_ground correctly in the if condition:

if drone(weight) <= ground(weight) and drone(weight) <= prem_ground:

prem_ground is already a float, so you can use it in the comparison directly. (without attempting to call a float like a function)