Sal's Shipping

weight = 8.4

#" Ground Shipping "
if weight <= 2:
cost_ground = weight * 1.5 + 20
elif weight > 2 or weight <= 6:
cost_ground = weight * 3.0 + 20
elif weight > 6 or weight < 10:
cost_ground = weight * 4.0 + 20
elif weight > 10:
cost_ground = weight * 4.75 + 20.0
print (cost_ground)
Here is my problem the first 2 print out the correct price but when i try the 8.4 pounds in the example it returns 45.2 instead of 53.6 that it should be and the 4th elif statement does not print the right amount also

The problem is that you are falling into the first elif. You are comparing if the weight is greater than 2 OR less than or equal to 6. Since you are using OR, if either condition is true, the whole condition is true. Since 8.4 is actually greater than 2, the whole condition is true, and it takes 8.4 * 3 and adds 20 to get 45.2. To make it work, you would need to use AND instead of OR.

All that said, though, you don’t need to check if weight is greater than 2 at all, since the first condition of the if statement already ensures that. You will only get to the first elif if the weight is greater than 2. So, your if statement could be simplified as the following. Also, the last elif could simply be an else, since by definition, you couldn’t get to this point unless the weight was greater than 10. (Note: the exercise does say “weight greater than 6 and less than or equal to 10.” That is also corrected below.)

if weight <= 2:
    cost_ground = weight * 1.5 + 20
elif weight <= 6:
    cost_ground = weight * 3.0 + 20
elif weight <= 10:
    cost_ground = weight * 4.0 + 20
else:
    cost_ground = weight * 4.75 + 20

Net, each of the elif sections only need to compare the next level, since you can’t get to that elif unless you already were greater than the earlier compares.

I hope that helps!

1 Like

Thank you that helped alot

1 Like

Here is my entire example, in case you want to check against yours as I read somewhere that the video tutorial is outdated.

weight = 41.5 # Ground Shipping if weight <=2: cost_ground = weight * 1.5 + 20 elif weight <=6: cost_ground = weight * 3 + 20 elif weight <=10: cost_ground = weight * 4 + 20 else: cost_ground = weight * 4.75 + 20 print("Ground Shipping Regular $", cost_ground) # Ground Shipping Premium cost_ground_premium = 125 print("Ground Shipping Premium $", cost_ground_premium) # Drone Shipping if weight <=2: cost_drone_shipping = weight * 4.5 elif weight <=6: cost_drone_shipping = weight * 9 elif weight <=10: cost_drone_shipping = weight * 12 else: cost_drone_shipping = weight * 14.25 print("Drone Shipping $", cost_drone_shipping)

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.