Sal's shipping help with code returning correct solution and a second output 'none'

Hi,

Sorry if this isn’t the correct format for a query, I have never posted here before. I have been trying to complete the project Sal’s shipping under the python control flow lessons (https://www.codecademy.com/paths/data-science/tracks/dspath-functions-and-logic/modules/dspath-control-flow/projects/sals-shipping). Basically I have completed the exercise and everything seemed to be working well. However for the last tasks (6 & 7) where you write a function to state the cheapest shipping method, the correct output is returned, but alongside another output “None” I’m not sure why and the guided walkthrough is so different from my solution that I cannot work out where the problem is in my code! I have enclosed the code for the full task below:

def ground_shipping_cost(weight):
  if weight <= 2:
     cost = 1.5 * weight + 20
  elif weight <= 6:
     cost = 3 * weight + 20
  elif weight <= 10:
     cost = 4 * weight + 20
  else:
      cost = 4.74 * weight + 20
  return cost
  
    
print(ground_shipping_cost(8.4))  

premium_ground_shipping = 125.00

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

print(drone_shipping_cost(1.5))

def price_calculator(weight):
  ground = ground_shipping_cost(weight)
  drone = drone_shipping_cost(weight)
  premium = premium_ground_shipping
  if ground < drone and ground < premium:
    cheapest = ground
    return print('The cheapest method is premium ground shipping at a cost of $' + str(cheapest) + '.')
  if drone < ground and drone < premium:
    cheapest = drone
    return print('The cheapest method is premium ground shipping at a cost of $' + str(cheapest) + '.')
  if premium < ground and premium < drone:
    cheapest = premium
    return print('The cheapest method is premium ground shipping at a cost of $' + str(cheapest) + '.')
  
print(price_calculator(4.8))

So for example when the print(price_calculator(4.8)) is executed it outputs:
“The cheapest method is premium ground shipping at a cost of $34.4.
None”

Sorry for the long post, if anyone could tell me why this extra “None” is printed I’d be really grateful, it seems the function does produce the correct value too.

If you call print twice maybe you should expect two things to get printed too? The obvious remedy would be to use print fewer times.

Also, that’s not a correct algorithm for determining the smallest of three, as it requires the value to be smaller than the two others (which isn’t the case if the two smallest values are equal) …and you only need to carry out two comparisons, not six

Thanks for your help and apologies I should have noticed that I had been calling print twice, that was easy to resolve.

In regards to the second part of your answer, would you suggest using elif and else to reduce the number of comparisons? I had considered the case in which two of the values were equal but I wasnt sure how best to address that. Again I had watched the walkthrough and I can’t see that it has been addressed there either, although I could be incorrect.

Thanks again for your help!

1 Like

Write down three numbers on a piece of paper.
Which one is smallest?
Observe how you figure that out.

It’s not a matter of knowing something terribly clever. This is a problem we know how to solve at an age of …idk a few months?
So the problem is more of skipping considering how it should be done. And that’s important to note I think, because considering what the code should do seems like the first thing to do, something that needs to happen before writing anything at all (otherwise, what’s being written?)