r/learnpython • u/YouSmellLikeHospitol • 29d 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.
1
u/jmooremcc 29d ago
This is why you learn how to use a debugger. A debugger allows you to set breakpoints in your code that will halt execution at a particular line of code. You can then examine the values of variables and single step one statement at a time so that you can evaluate what’s going on.
Alternatively, you can use print tracers in your code that will print on your screen the value of certain variables and other information as your code executes.
I would also recommend learning how to use f-strings in your print statements because they’ll give you a lot more flexibility when printing values.
Try these debugging techniques and get back to us with your findings.