Sal's Shipping Project

Here is my code:

weight = 8.4
#Ground Shipping
if weight <= 2:
cost_ground = weight * 1.5 + 20
elif weight > 3 and <= 6:
cost_ground = weight * 3 + 20
elif weight > 6 and <= 10:
cost_ground = weight * 4 +20
else:
cost_ground = weight * 4.75 +20

print(cost_ground)

I am getting this error:

File “shipping.py”, line 5
elif weight > 3 and <= 6:
^
SyntaxError: invalid syntax

Here is the link: https://www.codecademy.com/courses/learn-python-3/projects/python-sals-shipping

How is this invalid syntax? I am trying to “less than or equal to 6.” Not sure how that is invalid?

The carat is under the equal sign next to the 6. When I copied and pasted, it moved.

Unfortunately you can’t combine operators in this way. It would need to be split into two expressions like a > 2 and b <=5 or you could use python’s chaining comparisons (the syntax is closer to math: a < x < b) -
https://docs.python.org/3/reference/expressions.html#comparisons

Regarding the code the following link should be helpful (formatted code is much nicer)-
How do I format code in my posts?

1 Like

Ah, okay, so if I wrote weight < 2 and weight >=6, that works. Got it. Thank you!

Or you can write:

2 < weight <= 6