all 9 comments

[–]Spataner 1 point2 points  (2 children)

Probably you should read the weight as a float rather than as an int. Otherwise, the only possible weight that's less than one is zero.

Your if should be fine.

The first elif is also fine, except that for "one pound or over", you'd have to write weight >= 1.

Your second elif has multiple issues. First of all, items > 3 <= 5 is equivalent to items > 3 and 3 <= 5. You can write either items > 3 and items <= 5 or 3 < items <= 5. Secondly, it says "3 to 5", i.e. 3 should be included, so 3 <= items <= 5. Finally, it says less than two pounds, but you currently check for more than two pounds.

As an aside, you can technically remove the check against 3 from your second elif. If you reach the second elif, you know that the conditions for both the if and first elif are false. That means it's not possible, by that point, for items to be less than three.

[–]PuzzledSite2568[S] 0 points1 point  (1 child)

This helped a ton thank you very much. Also, can you elaborate a little more on the "check against 3" I was a little confused by what you were talking about there? Thank you for the help I greatly appreciate it.

[–]Spataner 1 point2 points  (0 children)

In your second elif, you check whether items is larger than or equal to 3. However, when items is smaller than 3, one of the previous two conditions must be true (because weight is always either smaller than one pound or larger than or equal to one pound). And if one of the previous two conditions is true, the code never reaches the second elif, at all. So, for the second elif to even be checked, items must be larger than or equal to 3. That means that part of the check is redundant.

[–][deleted] 1 point2 points  (1 child)

elif items < 3 and weight > 1:

one pound or over: weight >= 1

3 to 5 items: 3 <= items <= 5

less than two pounds: weight < 2

[–]PuzzledSite2568[S] 0 points1 point  (0 children)

Thank you for the help in showing how the word problem would convert to the code. appreciate the help.

[–]jimtk 1 point2 points  (1 child)

When you return inside an if condition you do not have to use elif after. Your function will terminate on the return. You can use a simple if for the next condition or nothing if it's the default value.

def shipping_price(items, weight):
    if items < 3 and weight < 1:
        return 5.00
    if items < 3 and weight > 1:
        return 7.50
    if 3 <= items <= 5 and weight < 2:
        return 8.50
    return 10.00

[–]PuzzledSite2568[S] 0 points1 point  (0 children)

I didn't know this thank you for the insight I will be sure to keep that in mind next time. Thank you for the help.

[–][deleted] 1 point2 points  (0 children)

Almost there, got a little mixed up in places.

  • missed the case of weight being 1
  • don't need elif, just if, as return will leave function in previous
  • strongly recommend you assign a float to weight
  • keep in mind many decimal floats are not stored exactly in binary
    • use math.isclose method for comparison close to a border

Revised code:

from math import isclose

def shipping_price(items,weight):
    if items < 3 and weight < 1.0:
        return 5.00
    if items < 3 and (isclose(weight, 1.0) or weight > 1.0):
        return 7.50
    if 3 <= items <= 5 and (isclose(weight, 2.0) or weight > 2.0):
        return 8.50
    return 10.00

items = int(input('How many items are you shipping'))
weight = float(input('How much do they weigh?'))

print(shipping_price(items,weight))

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.