Sal's shipping project question about result

Hi again,

Thanks for the help I have recieved here so far. I have another question today.
Reviewing the following code:

# Tasks doesn't ask for a random number, but I want to try to see it work. # import random # weight = random.randint(1.00, 50.00) # remove comment to update weight to fixed number weight = 0.5 # Ground Shipping flat_charge = 20 ground_premium_cost = 125.00 # Ground shipping price calculations: if weight <= 2: cost_ground = 1.50 * weight + flat_charge elif weight > 2 and weight <= 6: cost_ground = 3.00 * weight + flat_charge elif weight > 6 and weight <= 10: cost_ground = 4.00 * weight + flat_charge else: cost_ground = 4.75 * weight + flat_charge # Drone shipping price calculations: if weight <= 2: cost_drone = 4.50 * weight elif weight > 2 and weight <= 6: cost_drone = 9.00 * weight elif weight > 6 and weight <= 10: cost_drone = 12.00 * weight else: cost_drone = 14.25 * weight # Final price results: print(cost_ground) print(ground_premium_cost) print(cost_drone) if cost_ground < ground_premium_cost and cost_drone: print("The cheapest option is: " + str(cost_ground)) elif ground_premium_cost < cost_ground and cost_drone: print("The cheapest option is: " + str(ground_premium_cost)) elif cost_drone < cost_ground and ground_premium_cost: print("The cheapest option is: " + str(cost_drone)) else: print("Multiple options have the same price")

I have, as you can see, tried out some things that are not part of the assignment for fun, but why does the final result not suggest that drone_cost is cheapest, even when it is? would it be better to store the result in a “cheapest_price” variable and then print that?

Thanks in advance.

Because your first if condition is True.

if cost_ground < ground_premium_cost 
#if 20.75 < 125.0

You need to tweak that condition b/c the use of and (the boolean operator) is incorrect.
You want it (both sides of the expression) to evaluate to True, or True and True which = True

So, how would you fix that?

1 Like


This actually makes a lot of sense from what I know from formal logic (I have a philosophy background. So I guess it should be
if cost_ground < ground_premium_cost and cost_ground < cost_drone

I hope I am on the right track. Going to try it now!

1 Like

Yep. Check the elif and the else too. (even tho the first condition is met & won’t get to those).
A background in logic is complementary to learning to program! :slight_smile:

1 Like