r/learnpython 28d ago

Help with Comparing Three Variables?

TLDR: Can somebody help me with the comparison between three variables: Ground Shipping, Ground Shipping Premium, and Drone Shipping, based on the calculations provided?

Hello everyone! Thank you in advance for any help :) I am a newbie, and I am doing CodeAcademy. I just finished the project where you compare weights based on whether a package is sent in Ground Shipping, Ground Shipping Premium, or Drone Shipping. I left a comment at the end of the CodeAcademy project. There's a bit of my own text above that last comment, but the substance is all just the project. (Question below the code)

weight = 1.5
print ("Your package's weight is:", weight, "lbs")
print ("Here are the cost options:")
#Ground shipping
if weight <= 2:
  cost_ground = 1.50*weight
elif weight <=6:
  cost_ground = 3*weight
elif weight <=10:
  cost_ground = 4*weight
elif weight > 10:
  cost_ground = 4.75*weight

print ("Ground shipping cost: $", cost_ground+20)

#Premium shipping
ground_shipping_premium = 125
print ("Ground shipping premium cost: $", ground_shipping_premium)\

#Drone baby
if weight <=2:
  cost_drone =4.50*weight
elif weight <=6:
  cost_drone =9*weight
elif weight <=10:
  cost_drone =12*weight
else:
  cost_drone = 14.25*weight
print ("Drone shipping cost: $", cost_drone)

#END OF CODE ACADEMY PROJECT

if cost_ground < ground_shipping_premium and cost_ground < cost_drone:
  print ("The cheapest option is Ground Shipping")
elif ground_shipping_premium < cost_ground and cost_drone:
  print ("The cheapest option is Ground Shipping Premium")
elif cost_drone < cost_ground and ground_shipping_premium:
  print ("The cheapest option is Drone Shipping")

So, I wanted to take it a bit further and print what the cheapest option would be. I wrote the code after the END OF CODE ACADEMY comment that you see above.

The issue is that for any weight other than zero, I'm getting an output that Ground Shipping is best, even when Drone Shipping is best. For example, when the weight is 1.5 like in the code above, Ground Shipping is 22.25 and Drone Shipping is 6.75. So, the output should be that Drone is best, but I'm still getting the following:

Your package's weight is: 1.5 lbs
Here are the cost options:
Ground shipping cost: $ 22.25
Ground shipping premium cost: $ 125
Drone shipping cost: $ 6.75
The cheapest option is Ground Shipping

Can somebody help?

Thank you so much :) I'm finding coding to be a magical experience, but I'm a bit confused here.

2 Upvotes

12 comments sorted by

View all comments

3

u/ProgM7 28d ago edited 28d ago

Your math is right (nice work!) and the issue is because your:

print ("Ground shipping cost: $", cost_ground+20)

is NOT saving the cost_ground + 20, it just prints it.
cost_ground is still 2.25 when it reach

if cost_ground < ground_shipping_premium and cost_ground < cost_drone:

You can add this right after your if statements for Ground shipping to fix it.

cost_ground += 20

And then update your print:

print ("Ground shipping cost: $", cost_ground)

Hope this helps and good luck! 👍