Sal's Shipping. Can't figure out where the "none" value comes from

Hi! I gave it a try to this project on my own and I got the results, however there is a “none” that gets printed after the answer and I don’t know where it comes from. I tried to read my code several times and modified it, to see if I could find the reason behind it. Can you help my discover what’s the reason behind this please? My approach was different from the one of the video.
It is important to note that the ‘else’ section of my code is just to test other options in case they happen, you can delete it and it still works.
link of the project: https://www.codecademy.com/paths/data-science/tracks/dspath-functions-and-logic/modules/dspath-control-flow/projects/sals-shipping

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

def cost_drone_shipping(weight):
  if weight <= 2:
    return (4.5 * weight) 
  elif weight > 2 and weight <= 6:
    return (9* weight) 
  elif weight > 6 and weight <= 10:
    return (12 * weight) 
  else:
    return (14.25 * weight) 


premium_ground_shipping = 125

def cheap_shipping(weight):
  if premium_ground_shipping < cost_ground_shipping(weight) and premium_ground_shipping < cost_drone_shipping(weight):
    return print("The cheapest method is premium ground shipping and it costs $" + str(premium_ground_shipping))
  elif cost_ground_shipping(weight) <premium_ground_shipping and cost_ground_shipping(weight)< cost_drone_shipping(weight):
    return print("The cheapest method is  ground shipping and it costs $" + str(cost_ground_shipping(weight)))
  elif cost_drone_shipping(weight) <premium_ground_shipping and cost_drone_shipping(weight) < cost_ground_shipping(weight):
    return print("The cheapest method is  drone shipping and it costs $" + str(cost_drone_shipping(weight)))
  else:
    return print(str(cost_drone_shipping(weight)) +str(premium_ground_shipping(weight)) + str(cost_ground_shipping(weight)))

  

 

print(cheap_shipping(4.8))
print(cheap_shipping(41.5))

This is what gets printed:
The cheapest method is ground shipping and it costs $34.4
None
The cheapest method is premium ground shipping and it costs $125
None

Hi @axel.sof.edu,

The print function displays output in the console, but its return value is always None.

Your cheap_shipping function includes statements such as this one:

    return print("The cheapest method is premium ground shipping and it costs $" + str(premium_ground_shipping))

Therefore, when the following statement calls the cheap_shipping function, that function displays some output via one of its print statements, then returns the value of None from the print function. This calling statement then displays it:

print(cheap_shipping(4.8))

EDIT: Added the following on March 17, 2019:

Since the cheap_shipping function displays the necessary output, and its return value of None is of no particular interest to us, change the above to simply:

cheap_shipping(4.8)

Then the None will not be displayed. You can also revise all the print statements within the cheap_shipping function, so that they do not pass back the return value from them to the calling statement. For example, this:

    return print("The cheapest method is  drone shipping and it costs $" + str(cost_drone_shipping(weight)))

should be shortened to this:

    print("The cheapest method is  drone shipping and it costs $" + str(cost_drone_shipping(weight)))
2 Likes

Hi @appylpye!
Thank you very much for your reply, it helped me a lot. After following what you said the “none” disappeared. I think I have to get more familiar with Python yet. I thought I was supposed to print the function to get the answer, but like you said the function does already that. I don’t fully understand how the ‘return’ or even the ‘print’ statement work to a fully extent, but I’ll keep improving.

Thank you again.

1 Like

Hello @axel.sof.edu,

Within the Learn Python 3 instructional material, the print function is first discussed in the Learn Python: Syntax: Print exercise. Much additional detail about print is provided later on at various stages within the coursework.

The return statement is introduced in the Learn Python: Introduction to Functions: Returns exercise. For the best practice, it is probably most effective to review the entire Learn Python: Introduction to Functions course.