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.
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))
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.