Sal's Shipping - Potential Issue With Cheapest Shipping Calculation

https://www.codecademy.com/paths/build-chatbots-with-python/tracks/introduction-to-python-and-chatbots/modules/learn-python3-control-flow/projects/sals-shipping

I was wondering if some situations are not covered by the solution to which shipping method is cheapest.

We are required to use the following syntax:

def cheapest_shipping(weight):

ground = ground_shipping(weight)

premium = premium_ground_shipping

drone = drone_shipping(weight)

if ground < premium and ground < drone:

  method = "Ground Shipping"

  cost = ground

elif premium < ground and premium < drone:

  method = "Premium Ground Shipping"

  cost = premium

else:

  method = "Drone Shipping"

  costs = drone

If any shipping methods are equal, would the arguements fall apart? I’m not sure if I have articulated that well but in the syntax, we only specified if one cost was < another cost, but what happens when one method is equal to another method.
Eg. what if ground_shipping = premium_ground_shipping < drone_shipping
Wouldn’t that return drone shipping as the preferred shipping method because it is covered by the ‘Else’ statement?

I’m sure that this specific scenario doesn’t occur or it might not even be possible to occur in this task but shouldn’t we try to cover all of our bases anyway?

Why not try it out for yourself? Set ground and premium to the same value (that’s also less than drone) and see what happens! You’re correct, drone would be the cost returned. What changes could you make to the code to return ground or premium instead?


Asides from that, there are two other things I’d like to point out in your code. Firstly, you’re missing a return statement that will return method and cost.

Secondly, you’ve got a typo on the following line (should be cost and not costs).

Thank you so much for such a quick and helpful response @victoria_dr.

It did take a little head scratching to get ground and premium to equal which I did try to resolve before raising the question. I attempted to solve this by iteratively changing the value of cheapest_shipping(weight) so that it was $125 for both ground and premium but I don’t think this worked.

After your response I commented out a few lines of code and just changed the ground_shipping function to a variable that was also equal to 125. Do you think that was the most efficient way to check? I thought the numerous lines of commenting out looked a bit messy and might get confusing. I also just learnt that you can’t comment out multiple lines in python which is a bit annoying.

PS. thanks for picking up on the typo.

#def ground_shipping(weight):

#if weight <= 2:

#return (20 + (weight * 1.5))

elif weight <= 6:

return (20 + (weight * 3))

#elif weight <= 10:

return (20 + (weight * 4))

#else:

return (20 + (weight * 4.75))

ground_shipping = 125

premium_ground_shipping = 125

def drone_shipping(weight):

if weight <= 2:

return weight * 4.5

elif weight <= 6:

return weight * 9

elif weight <= 10:

return weight * 12

else:

return weight * 14.25

def cheapest_shipping(weight):

ground = ground_shipping#(weight)

premium = premium_ground_shipping

drone = drone_shipping(weight)

if ground < premium and ground < drone:

  method = "Ground Shipping"

  cost = ground

elif premium < ground and premium < drone:

  method = "Premium Ground Shipping"

  cost = premium

else:

  method = "Drone Shipping"

  cost = drone

print("The cheapest shipping method is %s Shipping, it will cost $%.2f."

% (method, cost)

)

cheapest_shipping(17)

cheapest_shipping(22.11)

cheapest_shipping(41.5)

Sorry, I forgot to address the main issue I raised. Is it as simple as adding <= in the cheapest_shipping functions if, elif, else statement?

I have just been trying to figure out all of the possible scenarios where different methods might equal or be smaller than each other and it sort of made my head spin a bit. Hopefully the answer is that simple and I just overthought it.

Thanks again.

In future posts with code, please format it using the </> button.


Why did you set ground to 125? This doesn’t solve the issue that if ground == premium, drone would be returned. It just makes it more obvious that there’s an issue. Walk step-by-step through your code if both ground and premium were 125 and drone was more than 125. Rather than returning ground or premium, drone would still be returned.


Have you tried this out for yourself? Run your code with this implemented and see if it works. Yes, it’s as simple as using <= rather than <!


You can surround your code with triple quotation marks (""") on both ends. This is effectively a multi-line string, but one that isn’t assigned to any variable, used in a function, etc. It acts like a multi-line comment.

Happy coding!

Thanks again @victoria_dr for your help and patience!

To explain why I tried to set ground to 125, it was my attempt to break the code and to test if the arguments fell apart which they did.

I’m not sure why I got so confused about all of this.

I did alter the code to include the = sign and it held up to my arguments but I wasn’t sure if I was missing something.

Also, I did already learn that you can use “”" to comment out multiple lines but I had somehow forgotten as i’m dabbling in a few languages. I did a quick google search and thought I saw it said somewhere that you can’t comment out multiple lines in Python.

Again, I cannot thank you enough for your patience, help and guidance.

Have a wonderful day! :smiley:

1 Like

Just to add this into the mix (as @victoria_dr mentioned):
While using """ to comment out multiple lines works fine, it is technically not a comment-it is a way to have a string over multiple lines. For example:

print("""
this
will 
print
""")

Will print:

this
will 
print
1 Like

Ahh, i’m with you now. I have just played around with it a little including setting a string over multiple lines and then trying to comment that out but actually as you said, it isn’t possible to comment out multiple lines in Python. Still, at least it helps. Thanks for the additional insight @codeneutrino. That’s really helpful.

As I say, I have definitely learnt this already but as i’m new to coding and trying to tackle a few languages, it’s a pretty steep learning curve and not everything is quite sticking all of the time yet.

Thanks.

2 Likes

No problem! Yes, I should have made it more explicit that """, does not in fact, make it a multi-line comment, but only can be used to comment out multiple lines.

Nice to see you’ve completed this project! :slight_smile:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.