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))
@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.
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.")