Sal's Shipping Project - None

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

def ground_cost(weight):
if weight <= 2:
cost = weight * 1.5 + 20
elif weight <= 6:
cost = weight * 3 + 20
elif weight <= 10:
cost = weight * 4 + 20
else:
cost = weight * 4.75 + 20
return cost

print(ground_cost(8.4))

prem_cost = 125

def drone_cost(weight):
if weight <= 2:
cost = weight * 4.5
elif weight <= 6:
cost = weight * 9
elif weight <= 10:
cost = weight * 12
else:
cost = weight * 14.25
return cost

print(drone_cost(1.5))

def cheap(weight):
if ground_cost(weight) < prem_cost and ground_cost(weight) < drone_cost(weight):
print("Ground Shipping is the cheapest method with the cost of " + str(ground_cost(weight))) elif ground_cost(weight) < prem_cost and not ground_cost(weight) < drone_cost(weight): print("Drone Shipping is the cheapest method with the cost of " + str(drone_cost(weight)))
else:
print(“Premium Ground Shipping is the cheapest method with the cost of $125.00”)

print(cheap(4.8))
print(cheap(41.5))

Why does it output None?
On a side note, how to quote code here?

None is the default object returned from a function which doesn’t have it’s own return statement. So if you print function calls which don’t return anything you’ll invariably end up with None printed to the screen one or more times.

As for code check this FAQ for details-

1 Like

Thanks for the answer.

I see I have print in my cheap function, then I print cheap function, so it ended up with 2 print. If I just call the function instead of print, would it fix the problem?

def cheap(weight):
  if ground_cost(weight) < prem_cost and ground_cost(weight) < drone_cost(weight):
    print("Ground Shipping is the cheapest method with the cost of $" + str(ground_cost(weight))) 
  elif ground_cost(weight) < prem_cost and not ground_cost(weight) < drone_cost(weight): 
    print("Drone Shipping is the cheapest method with the cost of $" + str(drone_cost(weight)))
  else:
    print(“Premium Ground Shipping is the cheapest method with the cost of $125.00”)

cheap(4.8)
cheap(41.5)

Or just change all the print in the cheap function to return instead

def cheap(weight):
  if ground_cost(weight) < prem_cost and ground_cost(weight) < drone_cost(weight):
    return "Ground Shipping is the cheapest method with the cost of $" + str(ground_cost(weight))
  elif ground_cost(weight) < prem_cost and not ground_cost(weight) < drone_cost(weight): 
    return "Drone Shipping is the cheapest method with the cost of $" + str(drone_cost(weight))
  else:
    return “Premium Ground Shipping is the cheapest method with the cost of $125.00”

print(cheap(4.8))
print(cheap(41.5))
2 Likes

Those are both valid solutions. I’d generally suggest using return over print for almost any function that didn’t have the sole purpose of outputting to the console, so almost all of them. Actually being able to use the value post function call gives you many more options.

1 Like