Sal's Shipping - Syntax Errpr

Hello all!

I’m doing a small project and I can’t figure out why I’m getting a syntax error for the following code:

f weight <= 2:
  cost_ground = weight * 1.5 + 20
elif weight >= 2 or <= 6:
  cost_ground = weight * 3.00 + 20
elif weight > 6 or == 10:
  cost_ground = weight * 4.00 + 20
else:
  cost_ground = weight * 4.75 + 20

The syntax error is as follows:

File “shipping.py”, line 7
elif weight >= 2 or <= 6:
^
SyntaxError: invalid syntax

As alywas, I’d be so grateful for some help!

Kind regards,
V

double check your values in your conditionals-- if, elif and comparison operators.

1 Like

Unfortunately, Python does not accept such syntax - the assumption that <=6 is also referring to weight. You need explicitly include weight:
elif weight >=2 or weight <=6:

Also, your condition doesn’t seems quite right. Do you mean weight between 2 and 6?
Suppose weight = 10, that condition will satisfy.

4 Likes

Thanks!! I explicitly added weight.

I meant greater than or equal to 2 or less than or equal 6. I’m getting the wrong calculation now so I’ve gone wrong somewhere.

I see where I’ve gone wrong. I was using or instead of and.

ive also been having a syntax error occur on me but i cant figure out why, mabey some one can help me out?

weight = 8.4

ground shipping prices

price_per_pound = 1.50
shipping_cost = weight * price_per_pound + 20
if weight <= 2:
elif weight > 2 or weight <= 6:
price_per_pound = 3.00
elif weight > 6 or weight <= 10:
price_per_pound = 4.00
else: weight > 10
price_per_pound
print(shipping_cost)

the error code is
File “shipping.py”, line 6
elif weight > 2 or weight <= 6:
^
SyntaxError: invalid syntax

ive had to rewrite it twice and i had it “right” so that line 6 was written the exact same way but now that i tried to fix the double answer i was getting in the output it says its not right, thanks in advance for the help.

Please see How do I format code in my posts? as it is difficult for others to read unformatted code and therefore to help.

Have you checked the indentation of this code? Was part of an if statement missed or have you tried to nest elif without an if statement (something like the following would be valid)-

if x == 3:
    if x != 10:  # if statement inside the other if is fine
        print("yes")
# an unindented elif is fine as it part of the first if statement
elif x == "red":
    print("no")
weight = 8.4
# ground shipping prices
price_per_pound = 1.50
shipping_cost = weight * price_per_pound + 20 
if weight <= 2:
 elif weight > 2 or weight <= 6:
  price_per_pound = 3.00
 elif weight > 6 or weight <= 10:
  price_per_pound = 4.00
else: weight > 10
price_per_pound
print(shipping_cost)