Sal's Shipping - Output for cheapest cost method error

I’ve written the below for Sal’s Shipping but the final result is coming up with the cheapest shipping method as Premium for both 4.8 lbs and 41.5 lbs. I know its correct for the 41.5 but I can’t figure out why it won’t say Ground for the 4.8. Code is below:

def ground_shipping(weight):
if weight <= 2:
return (weight * 1.50) + 20
elif weight > 2 and weight <= 6:
return (weight * 3) + 20
elif weight > 6 and weight <= 10:
return (weight * 4) + 20
elif weight > 10:
return (weight * 4.75) + 20

def drone_shipping(weight):
if weight <= 2:
return (weight * 4.50)
elif weight > 2 and weight <= 6:
return (weight * 9)
elif weight > 6 and weight <= 10:
return (weight * 12)
elif weight > 10:
return (weight * 14.25)

ground_cost = ground_shipping

premium_ground_cost = 125

drone_cost = drone_shipping

def cheapest_cost(weight):
if str(ground_cost) < str(premium_ground_cost) and str(ground_cost) < str(drone_cost):
return "Ground Shipping is the cheapest at " + str(ground_cost) + "." elif str(drone_cost) < str(premium_ground_cost) and str(drone_cost) < str(ground_cost): return "Drone Shipping is the cheapest at " + str(drone_cost) + “.”
elif str(premium_ground_cost) < str(ground_cost) and str(premium_ground_cost) < str(drone_cost):
return “Premium Ground Shipping is the cheapest at $” + str(premium_ground_cost) + “.”


Welcome to the forums!

What is this code right here doing? I see you have saved the two functions to ground_cost and drone_cost, but there isn’t any value being saved to it.

Why do you convert these to strings, (the ones in the if conditions)?

1 Like

Adding on to what @h1lo said right here, both your ground_cost and drone_cost functions take in a parameter, weight. Unless you implement default arguments, you have to provide an argument in the function call. Right now, the code is interpreted as assigning ground_cost and drone_cost the references of non-existent variables, which will throw an error.

And adding on to what was said here, your cheapest_cost function is the only thing being called. This means that your variable assignments for ground_cost, premium_ground_cost, and drone_cost should be inside the cheapest_cost function. This allows the cheapest_cost function to call the ground_shipping and drone_shipping functions with the correct function arguments.

Welcome to the forums!

1 Like

As an aside assigning function names without parentheses will simply provide you with a new reference to that function.

def number2str(number=1):
    return str(number)

strnumber = number2str

print(f'{number2str} is a {type(number2str)}')
print(f'{strnumber} is a {type(strnumber)}')

# python `ìs` keyword checks for identity (two different names/references for the same object)
print(f'{strnumber is number2str}')

test = strnumber()
print(f'This is the same function... {test}, {type(test)}')
test2 = number2str()
print(f'This is the same function... {test2}, {type(test2)}')

You’d need to be calling your function with the parentheses and the correct value for them to perform a you want them to, e.g. useful_return = func(value1, value2).


Thank you for the assistance! I needed to add the variable in the ground_shipping and drone_shipping; and remove the str() from the cheapest cost variables.