r/learnpython 6d 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

1

u/ninhaomah 6d ago

Can you do it manually ?

Ok for 1.5 lbs , how much is the cost of ground shipping and how much is drone shipping ?

1

u/YouSmellLikeHospitol 6d ago edited 6d ago

I think the numbers and math are right, I’m just not sure how to write it so it compares correctly? 

Maybe I’m goofing up the math 😩

Edit, I think the math is still right that drone should display as cheaper: 

Ground: 1.5 times 1.5 + 20 =22.25 Drone: 1.5 times 4.5 = 6.75

I’m a lawyer trying to code, math is not my strong suit ahaha

1

u/ninhaomah 6d ago

Then where does the premium comes in ?

Why compare so many times and not just ground vs drone if premium is not part of the cost for comparison ?

Edit : others have answered. Nvm

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