Python Control Flow - Sals shipping control task - Type error?

Hi all,
I have been struggling with this longer than I am happy to admit and have just about given up. can someone please point out why I keep getting this error. It is driving me insane. I’ve watched the help video and It doesn’t look too dissimilar, but I don’t want to just copy it and be left not knowing why mine doesn’t work.

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

Error:
Traceback (most recent call last):
File “script.py”, line 46, in
print(customer_package_cost(8.4))
File “script.py”, line 37, in customer_package_cost
if drone < ground and drone < premium:
TypeError: ‘<’ not supported between instances of ‘function’ and ‘function’

My problematic code:

def customer_package_cost(weight):
  drone = drone_shipping_cost
  ground = ground_shipping_cost
  premium = premium_ground_cost 
   
  if drone < ground and drone < premium:
    return "The cheapest method for shipping this item is Drone shipping. Costing $" + drone + "."

  elif ground < drone and ground < premium:
    return "The cheapest method for shipping this item is Ground shipping. Costing $" + ground +"."

  else:
    return "The cheapest method for shipping this item is Premium ground shipping. Costing $" + premium + "."

print(customer_package_cost(8.4))

My ‘understanding’ of the error:
I think/thought it was was flagged due to me using “<” with two variables but I’ve tried multiple things and cannot for the life of me figure out what is wrong. I’ve checked the help video he is also comparing variables. I’ve now played with/altered this code so many times I’m on the verge of copying the video completely and moving on with no further understanding of what went wrong.

any help would be greatly appreciated

Cell

1 Like

I like how you try to figure what the error is. I think that is a good thing

python tells us, that the smaller then operator (<) can’t be used to compare two functions.

the problem is here:

drone = drone_shipping_cost

you never call/invoke the shipping cost function. So instead of containing the returned result (a float/integer), the variable now contains the function itself.

1 Like

Thanks for the help. That finally worked! after 5+ hours of trying…

I’m only getting values returning to the first decimal place so I now have a new problem to figure out, which I don’t remember learning about.

Thanks again

Hello, I too got stuck into this part of the exercise and I understood the walkthrough video but it used “$%.2f” and “%s” which we hadn’t learnt before and I’m not sure if I will remember those the day I’ll need them. When I tried doing the exercise, I did something similar to what you did and I didn’t understand what was wrong :confused:

1 Like

def customer_package_cost(weight):
drone = drone_shipping_cost(weight)
ground = ground_shipping_cost(weight)
premium = premium_ground_cost

if drone < ground and drone < premium:
return “The cheapest method for shipping this item is Drone shipping. Costing $” + drone + “.”

elif ground < drone and ground < premium:
return “The cheapest method for shipping this item is Ground shipping. Costing $” + str(ground) +"."

else:
return “The cheapest method for shipping this item is Premium ground shipping. Costing $” + str(premium) + “.”

print(customer_package_cost(4.8))

prints $34.4

print(customer_package_cost(41.5))

prints 125.0

1 Like

forgot to put str(drone), and when you print and call the function twice, it now prints: The cheapest method for shipping this item is Ground shipping. Costing $34.4.
The cheapest method for shipping this item is Premium ground shipping. Costing $125.0.

1 Like

Did you manage to resolve your issue? :slight_smile:

Yes, thanks, I did it using the longer way because I know that “$%.2f” and “%s” aren’t things I’ve learnt before, even though it’s quicker.

The recommend approach is to learn about concepts you don’t know. You never know when you might need them or run into them (if you take over an existing code base)

2 Likes

well I didn’t solve the problem even though my coding is similar to him! would you please help me ?Capture|690x442

working from screenshots is really difficult. Please copy paste your code to the forum

def cheapest_shipping(weight):

  ground = ground_shipping(weight)

  premium = premium_ground_shipping

  drone = drone_shipping(weight)

  if premium < ground and premium < drone:

     return "premium ground shipping is the cheapest and it cost $" + str(premium)

  elif ground < premium and ground < drone:

      return "ground shipping is the cheapest and it cost $" + str(ground)

  else:

      return "drone shipping is the cheapest and it cost $" + str(drone)

print(cheapest_shipping(4.8))

print(cheapest_shipping(41.5))

but what if one of the shipping cost functions doesn’t return the right value? That could be, given None can be the absence of a return value.

The values you use for the comparison come from other functions. So without ensuring those function work right, there is no way I can debug this code

def ground_shipping(weight):

  if weight <= 2.00:

    Price_per_pound = 1.50

  elif weight > 2.00 and weight <= 6.00:

    Price_per_pound = 3.00

  elif weight > 6.00 and weight <= 10.00:

    Price_per_pound = 4.00

  elif weight > 10.00: 

    Price_per_pound = 4.75

    return weight*Price_per_pound + 20

print(ground_shipping(11))

premium_ground_shipping = print(125)

def drone_shipping(weight):

  if weight <= 2.00:

    Price_per_pound = 4.50

  elif weight > 2.00 and weight <= 6.00:

    Price_per_pound = 9.00

  elif weight > 6.00 and weight <= 10.00:

    Price_per_pound = 12.00

  elif weight > 10.00: 

    Price_per_pound = 14.25

    return weight*Price_per_pound + 20

print(drone_shipping(11))

def cheapest_shipping(weight):

  ground = ground_shipping(weight)

  premium = premium_ground_shipping

  drone = drone_shipping(weight)

  if premium < ground and premium < drone:

     return "premium ground shipping is the cheapest and it cost $" + str(premium)

  elif ground < premium and ground < drone:

      return "ground shipping is the cheapest and it cost $" + str(ground)

  else:

      return "drone shipping is the cheapest and it cost $" + str(drone)

print(cheapest_shipping(4.8))

print(cheapest_shipping(41.5))

something else, pep 8 guidelines say that variable and function names should be all lowercase

I think you should test your shipping (cost) functions some more:

def ground_shipping(weight):

  if weight <= 2.00:

    Price_per_pound = 1.50

  elif weight > 2.00 and weight <= 6.00:

    Price_per_pound = 3.00

  elif weight > 6.00 and weight <= 10.00:

    Price_per_pound = 4.00

  elif weight > 10.00: 

    Price_per_pound = 4.75

    return weight*Price_per_pound + 20

print(ground_shipping(1))
print(ground_shipping(4))
print(ground_shipping(8))
print(ground_shipping(11))

see? You get None (no return value) for most of the cases in your function.

Oh I see, thanks! I still can’t run the other half of the coding, even though I fixed the Uppercase problem. Here is the error:

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

The code i posted only demonstrated the problem. Do you understand the problem? You still need to fix it

Thank you so much! I fixed the problem! I am sorry that I misunderstood what you mean just now.