# Sal's Shipping - confused about elif/if

Hi all, I am confused by the elif/if code in the solution to Sal’s Shipping Project

For the final function, to find the cheapest shipping option and return text that says which option is cheapest and how much it will cost for a given weight, here is my code (very similar to the solution given in the video tutorial walkthrough:

``````def print_cheapest_method(weight):
ground = ground_shipping(weight)
drone = drone_shipping(weight)

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

else:
method = "drone shipping"
cost = drone

print(
"the cheapest shipping method is %s, it will cost you \$%.2f"
% (method, cost))```

I used if/if/else for my control flow.

When I answer the question: "What is the cheapest method of shipping a 4.8 pound package and how much would it cost?"

My function returns the incorrect answer:
"the cheapest shipping method is drone shipping, it will cost you \$43.20"

But if I change the control flow to if/elif/else, the function returns the correct answer:
"the cheapest shipping method is ground shipping, it will cost you \$34.40"

I really can't figure out why this is the case, like why the elif is necessary here. Especially since the correct answer is ground shipping, which comes first. If anyone can explain I'd appreciate it, thanks!``````

Hello! The reason you get the wrong answer when using `if…if…else` is that the `else` will run unless the second `if` is `True`:

``````if ground < premium and ground < drone:
#if this condition is True, the following code will run:
method = "ground shipping"
cost = ground
#if this condition is True, the following code will run:
else:
#If the above if statement evaluates to False-even if the first if statement evaluates to true, the following code will run:
method = "drone shipping"
cost = drone
``````
1 Like

Thanks! That does make sense, that the “else” condition is triggered because the condition immediately before it is false, but wouldn’t that mean that it would also return the answer ground shipping (the first condition) because that is true? It seems like that is what happened here in this similar question thread (Sal's Shipping Exercise - why use elif?), where the fellow asking the question got two shipping methods returned. But in my case it only returned the final “else” condition (drone) even though the first condition (ground) should have registered as true.

Consider where the output is. You won’t get it until you ask for it and if your values change before then you’ll only get the current state. For a very over-simplified example-

``````x = 3
x = 4
print(x)
Out: 4
``````
1 Like

Interesting thanks. I hope more examples come up like this in the exercise, I’d like to get more of a feel for it.