# I'm having trouble with a cascading if statement (code is executing on a false expression)

This is the link to the project I’m having trouble with.

Below is the snippet that is giving me an erroneous result.

``````# Cheapest shipping method
print()
if (cost_ground < cost_premium) and (cost_ground < cost_drone):
print("The most cost effective shipping method for your package is ground shipping.")
print("The most cost effective shipping method for your package is ground shipping premium.")
elif (cost_drone < cost_ground) and (cost_drone < cost_premium):
print("The most cost effective shipping method for your package is drone shipping.")
``````

For some reason the code is executing the following expression even when it’s false.

``````elif (cost_premium < cost_ground) and (cost_premium < cost_drone):
print("The most cost effective shipping method for your package is ground shipping premium.")
``````

No matter what number I input the result is always: “The most cost effective shipping method for your package is ground shipping premium.” Am I using the comparison operators wrong?

Below is the full code if you’d like to run it yourself or have more context.

``````print("Cost effectiveness calculator")
print()

weight = 4.67

# Cost of ground shipping
cost_ground = None
if weight <= 2:
cost_ground = "{:.2f}".format(20 + (weight * 1.50))
elif weight <= 6:
cost_ground = "{:.2f}".format(20 + (weight * 3.00))
elif weight <= 10:
cost_ground = "{:.2f}".format(20 + (weight * 4.00))
elif weight > 10:
cost_ground = "{:.2f}".format(20 + (weight * 4.75))
print("Your total cost for ground shipping is: \$" + str(cost_ground))

# Cost of ground shipping premium

# Cost of drone shipping
cost_drone = None
if weight <= 2:
cost_drone = "{:.2f}".format(weight * 4.50)
elif weight <= 6:
cost_drone = "{:.2f}".format(weight * 9.00)
elif weight <= 10:
cost_drone = "{:.2f}".format(weight * 12.00)
elif weight > 10:
cost_drone = "{:.2f}".format(weight * 14.25)
print("Your total cost for drone shipping is: \$" + str(cost_drone))

# Cheapest shipping method
print()
if (cost_ground < cost_premium) and (cost_ground < cost_drone):
print("The most cost effective shipping method for your package is ground shipping.")
print("The most cost effective shipping method for your package is ground shipping premium.")
elif (cost_drone < cost_ground) and (cost_drone < cost_premium):
print("The most cost effective shipping method for your package is drone shipping.")
``````

Thank you for your time and please let me know if I haven’t given enough details or if I formatted something wrong. This is my first time posting on the forum.

Can you please post all of your code? You are missing the function definitions…which would help one see where the errors are.

1 Like

Hi, thanks for your response. I did post the whole code. The codecademy course hasn’t mentioned anything about “function definitions” yet so I don’t know what that is. I’ll go research them and get back to you.

My mistake! I guess they’ve refreshed Sal’s Shipping.
(The previous version had function definitions). Ex:

``````def normal_ground(weight):
flatcharge = 20.0

if (weight <= 2.0):
return flatcharge + (weight * 1.50)

elif (weight <=6.0):
return flatcharge + (weight * 3.00)
``````
2 Likes

Be careful with your types, the comparison for a string behaves differently to that of an integer. Double-check what types you’re actually working with.

2 Likes

Thank you guys so much! I figured it out with your advice. I was changing the integers into strings with the “{:.2f}”.format function. I used it because I wanted to have the numbers expressed with two decimals to represent cents. I guess this is the danger of using things that you don’t understand in code. I’ll be more careful next time.

2 Likes