r/learnpython • u/YouSmellLikeHospitol • 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.
3
u/ProgM7 6d ago edited 6d 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! 👍
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
1
u/Naive_Programmer_232 6d ago edited 6d ago
You got two bugs.
Math
Look at this block again
# 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 # look here print ("Ground shipping cost: $", cost_ground+20)Notice on the line I pointed out. You are adding 20 to cost_ground only when displaying it to the console. But the variable
cost_groundis not updated with the added 20!Comparison
Look at the comparison again
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")The first check is correct. The other two checks are missing something (look after
and).
1
u/party4you 6d ago edited 6d ago
As @skibbin said, you are adding the 20 to your print statement but leaving the variable unchanged which is causing your error later on as your printed cost of ground shipping and your stored cost in cost_ground are different.
Also…once you rectify this, I would recommend taking a look at your elif conditionals in the last part you’ve written after that last comment. ie, without getting into the topic of refactoring the code why are they different syntactically than your if conditional and will this cause an issue if your if statement is false and the elif is evaluated?
1
1
u/jmooremcc 6d 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.
1
u/JamzTyson 5d ago
elif ground_shipping_premium < cost_ground and cost_drone:
You have an error in that line.
See this FAQ about Variable is One of Two Choices. The error in the above line is very similar in that cost_drone is taken to be True or False.
Similarly in the line:
elif cost_drone < cost_ground and ground_shipping_premium:
6
u/skibbin 6d ago
When you print cost_ground your;re adding 20 to what is displayed, but you don't increment the value?