Hi all, I am confused by the elif/if code in the solution to Sal’s Shipping Project
https://www.codecademy.com/courses/learn-python-3/projects/sals-shipping
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)
premium = premium_ground_shipping
drone = drone_shipping(weight)
if ground < premium and ground < drone:
method = "ground shipping"
cost = ground
if premium < ground and premium < drone:
method = "premium shipping"
cost = premium
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!