Sal's Shipping

I entered the following code

def ground_shipping(weight):
if weight <= 2:
price_per_pound = 1.5
elif weight <= 6:
price_per_pound = 3
elif weight <= 10:
price_per_pound = 4
else:
price_per_pound = 4.75

cost = (price_per_pound * weight) +20
return cost

print(ground_shipping(8.4))

premium_shipping_cost = 125.00

def drone_shipping(weight):
if weight <= 2:
price_per_pound = 4.5
elif weight <= 6:
price_per_pound = 9
elif weight <= 10:
price_per_pound = 12
else:
price_per_pound = 14.25

return price_per_pound * weight

print(drone_shipping(1.5))

def cheapest_shipping(weight):
ground = ground_shipping(weight)
premium = premium_shipping_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 is $%.2f with %s shipping.”
% (cost, method)
)

print(cheapest_shipping(4.8))
print(cheapest_shipping(41.5))

and my result was as follows:

53.6
6.75
The cheapest option is $34.40 with standard ground shipping.
None
The cheapest option is $125.00 with premium ground shipping.
None

Could someone explain to me why ‘None’ is appearing in the result and how to remove it?

1 Like

Hello @sanmeet, welcome to the forums! The reason is in this:

Here, you print the result, but you do not return anything, and then you print the result of calling the function here:

When you print a function that has no return within it, you call the function so any print statements within the function are executed, however None is also printed, since nothing has been returned.
I hope this helps!

3 Likes

Hey! I wont be much help to you but when I was coding the drone I came up with this catchy little number
def drone_shipping(weight): return ((ground_shipping(weight)-20)*3)

it makes the ground shipping prince do all the work for you and saves rewriting alot of code.
the reason for all the brackets is we want to remove the flat rate from the ground_shipping BEFORE multiplying it by 3 (since the intro said drone shipping rate is 3 times ground but with no flat rate)

I think its neat we named our variables the same. I really wish I had named them something shorter
I should have taken a page out of your book and renamed the variables in the cheapest_shipping function. I had to do alot of typing!

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.