Python Control Flow Assignment

Hi everyone,
I’m having this problem that I’m just don’t understand why this is happening. I tried to Google and StackOverflow for this problem and didn’t have a clear answer. Here’s the code:

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


premium_ground_shipping = float(120.0)

def drone_shipping(weight):
  if weight <= 2:
    print(float(4.5*weight))
  elif weight > 2 and weight <= 6:
    print(float(9*weight))
  elif weight > 6 and weight <= 10:
    print(float(12*weight))
  else:
    print(float(14.25*weight))
  
    
def cheapest_shipping(weight):
  ground = ground_shipping(weight)
  premium = premium_ground_shipping
  drone = drone_shipping(weight)
  if ground < premium and ground < drone:
    print("Ground shipping is the cheapest method, it costs $" + str(ground))
  elif drone < ground and drone < premium:
    print("Drone shipping is the cheapest method, it costs $" + str(drone))
  else:
    print("Premium ground Shipping is the cheapest, it costs $" + str(premium))
  
cheapest_shipping(41.5)

The problem is that when I run it, the error is:
Traceback (most recent call last):
File “script.py”, line 36, in
cheapest_shipping(41.5)
File “script.py”, line 29, in cheapest_shipping
if ground < premium and ground < drone:
TypeError: ‘<’ not supported between instances of ‘NoneType’ and ‘float’

I don’t understand why using a defined function to another function change the defined function to NoneType. My code should be really straightforward and I thought it should work just fine, but I can’t understand the problem. Thanks for answering and have a great day!

A bit of explanation on what did I tried, at first, I thought there’s no type consistency between the conditions in the functions (for example, the ground_shipping function has the 1st condition print out float, but the second condition prints out integer), so I decided to assigned float to all the result. If there’s any bug in this thinking, please let me know! Thank you!

Hi @bytemaster49288 - welcome to the forums.

Could you please edit your post and use the forum’s “code formatting” option? Code that is posted without the indentations, whitespace and special characters preserved, is very difficult to read, and cannot easily be tested by those trying to help you. This is especially the case with Python, where the indentation is critical.

The Codecademy forum provides a quick and easy way to post properly-formatted code. All you need to do is look for the </> icon in the menu bar which appears at the top of the text box while you type.

capture

Press Enter to go to a blank line, click that icon, and you will see this:

capture_r

Just copy and paste your code directly from the editor into that highlighted portion, and all of the nicely-readable formatting will be preserved! Moreover, it will be in a format that can be directly copied and pasted into an IDE for testing.

Edit: Actually, whilst you should make a habit of providing your code in a manner that makes it easy for us to run and test it, it’s not necessary in this instance.

Neither of your functions ground_shipping() or drone_shipping() contain a return statement. As a result, you’ll get Python’s default return value which is None.

Hence, when you’re initialising your ground variable for example:
ground = ground_shipping(weight)

ground is given the value of None, because there’s no other value returned from the function. Thus, when you try and compare them by doing ground < premium, you’re comparing a NoneType with a float and rightly getting a TypeError.

Hi thepitycoder, so based on what you’re explaining, what I should do is just replacing print with return in both functions, and it should be good? And also, sorry for the bad code formatting, I don’t see the edit post button on the main post to change it.

Understanding what the differences print() and return should go a long way in understanding what you need to use and why. Could you explain the difference between return and print for me?

you might need to press the triple dots to display more options