0

Below is the code, I want to the program to display something after verifying the variable status is between 0 to 20.

status = 12
if (status >= 0 & <= 20):
   print("something")
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Pistone Sanjama
  • 489
  • 1
  • 4
  • 14
  • 3
    What do you think ``& <= 20`` means? Did you intend to test ``0 <= status <= 20``? – MisterMiyagi Jun 08 '20 at 18:52
  • Does this answer your question? [Python's equivalent of && (logical-and) in an if-statement](https://stackoverflow.com/questions/2485466/pythons-equivalent-of-logical-and-in-an-if-statement) – Abhinav Goyal Jun 08 '20 at 18:55
  • No i want to test if the status is in between 0 to 20. I have tried "and" and it is not working also. The idea thoeretically is "If status is equal to or greater than 0 and equal to or less than 20 display something" – Pistone Sanjama Jun 08 '20 at 19:01
  • You could do `status >= 0 & status <= 20`, or `status >= 0 and status <= 20` or `0 <= status <= 20` – tdelaney Jun 08 '20 at 19:03
  • Related: [Determine Whether Integer Is Between Two Other Integers?](https://stackoverflow.com/q/13628791/4518341) – wjandrea Jun 08 '20 at 19:28

2 Answers2

2

Yes, this is a syntax error. Both & and and (which is the one you should be using) expect two expressions as operands, and <= 20 is not a valid expression.

if status >= 0 and status <= 20:

However, comparison operators are parsed specially to allow for chaining of comparisons.

0 <= status <= 20

is not parsed as nested expressions like (0 <= status) <= 20

>>> ast.dump(ast.parse('(0 <= status) <= 20'))
"Module(body=[Expr(value=Compare(left=Compare(left=Num(n=0), ops=[LtE()], comparators=[Name(id='status', ctx=Load())]), ops=[LtE()], comparators=[Num(n=20)]))])"

or 0 <= (status <= 20)

>>> ast.dump(ast.parse('0 <= (status <= 20)'))
"Module(body=[Expr(value=Compare(left=Num(n=0), ops=[LtE()], comparators=[Compare(left=Name(id='status', ctx=Load()), ops=[LtE()], comparators=[Num(n=20)])]))])" 

, but as a single expression consisting of two comparison operations.

>>> ast.dump(ast.parse('0 <= status <= 20'))
"Module(body=[Expr(value=Compare(left=Num(n=0), ops=[LtE(), LtE()], comparators=[Name(id='status', ctx=Load()), Num(n=20)]))])"

The semantics are nearly identical to the semantics of 0 <= status and status <= 20, which the difference being that status is only evaluated once.


In general, x OP1 y OP2 z is equivalent to x OP1 y and y OP2 z, where each of OP1 and OP2 can be one of >, <, ==, !=, >=, <=, is, is not, in, or not in. Most of the combinations are less readable than an explicit conjunction of tests; stick with "natural-looking" combination like x < y <= z, x < y == z, etc.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

Try replacing & with and. In python, there is no &&, there is only and. In addition, when doing the and operator, each side of the and should be a valid comparison. In the if statement, the first thing that is compared is status >= 0, which returns a boolean. However, in the next part of the if statement, you put <= 20, which wouldn't return anything, as it is not a valid comparison. Each part of the if statement should return a boolean value. The code below should solve your problem.

status = 12
if status >= 0 and status <= 20:
   print("something")

or

status = 12
if 0 <= status <= 20:
   print("something")
wjandrea
  • 28,235
  • 9
  • 60
  • 81
mihirm
  • 1
  • 2
  • *"which wouldn't return anything, as it is not a valid comparison"* - That's a confusing way to put it, cause Python raises the SyntaxError before anything gets evaluated, so talking about returning is beside the point. – wjandrea Jun 08 '20 at 19:24