Sal's shipping python control flow

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-control-flow/projects/sals-shipping
Please help me get unstuck:

I had gone through related questions and found a similar "weight not defined"problem, but unlike it I don’t have a typo.
I had it my way without converting functions to variables then had them compared and of course I ran into trouble. So I learned from the video which led me to this
“line 28, in
ground=cost_ground(weight)
NameError: name ‘weight’ is not defined”

here is my code and sorry for not knowing how to format

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

cost_permium=125

def cost_drone(weight):
  if weight<=2:
    return weight*4.5
  elif 2<weight<=6:
    return weight*9
  elif 6<weight<=10:
    return 12*weight
  else:
    return 14.25*weight
print(cost_drone(1.5))

ground=cost_ground(weight)
drone=cost_drone(weight)
premium=cost_premium()

def cheapest_way(weight):
  if ground<premium and ground<drone:
    print("Cost for ground shipping is the cheapest, and it costs "+ str(cost_ground)+" dollar. ")
  if drone<premium and drone<ground:
    print("Cost for drone shipping is the cheapest, and it costs "+ str(cost_drone)+" dollar.")
  if premium<drone and premium<ground:
    print("Cost for premium shipping is the cheapest, and it costs 125 dollar.")
    
print(cheapest_way(4.8))

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

Hi @array3604024044! Can you please include the link to the exercise you are working on and format your code like the post below?

2 Likes

@array3604024044 To properly format your code, please use the </> icon that is in the menu bar that appears atop the text box when you open it to type. You can do this right now via an edit to your post.
capture

3 Likes

Thanks for the tip. Much appreciated!

1 Like

Those three variables are only used inside the function so might as well be declared in there, as opposed to being global.

Note also that this will solve the issue of undefined variable, weight.

Aside

As an example, a suggested best practice in programming is to keep operators visible by surrounding them with white space.

elif 6 < weight <= 10
    return weight * 12

See how visible the operators are?

1 Like

Thanks for pointing things out. It was stupid of me to think its efficient to skip spaces around operators.
I did put 3 variables inside each function, and now the new prompt says this:NameError: name ‘ground’ is not defined

def cheapest_way(weight):
    if ground < premium and ground < drone:
        print("Cost for ground shipping is the cheapest, and it costs " + str(cost_ground) + " dollar. ")
    elif drone < premium and drone < ground:
        print("Cost for drone shipping is the cheapest, and it costs " + str(cost_drone) + " dollar.")
    elif premium < drone and premium < ground:
        print("Cost for premium shipping is the cheapest, and it costs 125 dollar.")
1 Like

You still need to install those variables inside the function…

ground = ground_cost(weight)
etc.
if ground ...

I already did, I am confused. Thanks for taking your time with me

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

That line should be in the other function (cheapest_cost). Declare all three variables in that function.