Question on Sal's Shipping Python Project

Can anyone enlighten me on my coding? I cant display Line 47. Been trying to debug this for an hour, but I’m still learning the basics of python. Thank you in advance!


What’s the error message that you receive? Were you able to print results for each chunk of code that you wrote (rather than running everything at once)?

When I try to debug something sometimes I find it best to read from the bottom up. (It’s a different way to look at it.)

There is no error prompt. Just that line 47

(cheapest_shipping(4.8))

is not printed when I run my code. However, it did work if I run

print(gship(4.8))

This is my full code:

def gship(weight):
  if weight <= 2:
    price = 1.5
  elif weight <= 6:
    price = 3.0
  elif weight <= 10:
    price = 4.0
  else:
    price = 4.75

  return ((price * weight) + 20)

pship = 125.0 #thank you lisa!

def dship(weight):
  if weight <= 2:
    price = 4.5
  elif 2 < weight <= 6:
    price = 9.0
  elif 6 < weight <= 10:
    price = 12.0
  else:
    price = 14.75

  return (price * weight)

def cheapest_shipping(weight):
  ground = gship(weight)
  drone = dship(weight)
  premium = pship

  if drone > ground and ground < premium:
    cost = ground
    method = "ground shipping"
  elif ground > drone < premium:
    cost = drone
    method = "drone shipping"
  else:
    cost = premium
    method = "premium shipping"

    print (
      "The cheapest option available is $%.2f with %s."
        % (cost,method)
    )

cheapest_shipping(4.8) #this line doesn't print out.
cheapest_shipping(41.5)
  

Hi, can you please format your code when posting (select “</>”) it’s easier to read when it’s properly indented.

Also, pship is supposed to be 125.0 not 120.0

Did you call the function for the first chunk of code? did it work?
ie: print(gship(5.3))

When I call your functions, the 3rd one does return something for the weight of 41.5:

def cheapest_shipping(weight):
    ground = gship(weight)
    drone = dship(weight)
    premium = pship

    if drone > ground < premium:
        cost = ground
        method = "ground shipping"
    elif ground > drone < premium:
        cost = drone
        method = "drone shipping"
    else:
        cost = premium
        method = "premium shipping"

        print("The cheapest option available is $%.2f with %s."
        % (cost, method))


print(cheapest_shipping(41.5))

Sorry! I have changed my code using </>. And thank you for your quick response, really appreciate it.
The problem still persists though, if i try to print

cheapest_shipping(4.8)

Sure, np. I definitely need more coffee. :slight_smile:

If you’re getting None it means that the conditions aren’t being met/won’t evaluate to True in the function.

See:
https://discuss.codecademy.com/t/sals-shipping-project-python/437758/2

This about the construction of if elif else in the 3rd function.
What could you write/change so the statement evaluates to True?

Look back over the previous less on Control Flow and the last exercise about Calvin Coolidge’s Cool college application admissions. How did you construct those if elif else statements?

1 Like

ITS THE INDENTATION!

def cheapest_shipping(weight):
    ground = gship(weight)
    drone = dship(weight)
    premium = pship

    if drone > ground < premium:
        cost = ground
        method = "ground shipping"
    elif ground > drone < premium:
        cost = drone
        method = "drone shipping"
    else:
        cost = premium
        method = "premium shipping"

    print("The cheapest option available is $%.2f with %s."
        % (cost, method)) #this has an additional indentation! 

:scream:

Thank you Lisa!

Good catch.
(I had reformatted it when I wrote it out in Colab).

But, what happens when you plug in other weights for that function? 4.8? 13.4? etc.

It works too!
Thank you for your help.

1 Like

Sure, n.p.
It’s also good to read up on indentation in the docs here:

https://www.python.org/dev/peps/pep-0008/#indentation

Happy coding! :partying_face: