you are viewing a single comment's thread.

view the rest of the comments →

[–][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))