Sals Shipping

Hello,

Been working on the Sals Shipping problem. Been stuck for hours trying to figure out where I went wrong. I watched the video demonstration and tried copying the code directly from the video to see what working code would look like. Problem is I get an error even when attempting to copy the developers code into the lab. Below is the code I have along with the error I am getting:

def cost_of_ground(weight):

if weight > 10:
  price_per_pound = 4.75
elif weight > 6:
  price_per_pound = 4.00
elif weight > 2:
  price_per_pound = 3.00
else:
  price_per_pound = 1.50
  
  return 20 + (price_per_pound * weight)

premium_ground_shipping = 125.00

def cost_of_drone(weight):

if weight > 10:
  price_per_pound = 14.25
elif weight > 6:
  price_per_pound = 12.00
elif weight > 2:
  price_per_pound = 9.00
else:
  price_per_pound = 4.50
  
  return price_per_pound * weight

def print_cheapest_shipping_cost(weight):

ground = cost_of_ground(weight)
premium = premium_ground_shipping
drone = cost_of_drone(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 available is $%.2f with %s shipping.”
% (cost, method)
)

print_cheapest_shipping_cost(41.5)

ERROR:

Traceback (most recent call last):
File “script.py”, line 54, in
print_cheapest_shipping_cost(41.5)
File “script.py”, line 39, in print_cheapest_shipping_cost
if ground < premium and ground < drone:
TypeError: ‘<’ not supported between instances of ‘NoneType’ and ‘float’

What am I doing wrong and how how do I fix this? It seems that I may not be defining the variables ground, premium, and drone correctly. Here is the link to the problem on CodeCademy:

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-control-flow/projects/sals-shipping

Thanks!

Your both of return statements in functions cost_of_ground(weight) and cost_of_drone(weight) need to be indented on the same level as your if and else statements. It should look like this:

if weight > 10:
  price_per_pound = 4.75
elif weight > 6:
  price_per_pound = 4.00
elif weight > 2:
  price_per_pound = 3.00
else:
  price_per_pound = 1.50
  
return 20 + (price_per_pound * weight)
1 Like

And another thing. You can improve the readability and speed of your program by using f-string in your print() function in print_cheapest_shipping_cost(weight) function. Instead of using the % sign, you can do this:

print(
   f"The cheapest option available is ${cost:.2f} with {method} shipping."
  )

It will not affect the final output, just will make code easier to read, and improve the speed of your program.

1 Like

Thank you so much!!! Always cracks me up that the cause of 5 hours staring at a screen was because of something trivial as a simple indent syntax mistake. Didn’t even think to look for indent errors when I wasn’t getting an indent error code. Thanks again! I can now sleep again.

1 Like

You are very welcome :smiley: