简体   繁体   English

当我在 python 的 if 语句中组合按位和逻辑运算符时,出现语法错误,说明语法无效

[英]Am getting a Syntax error saying invalid syntax when i combine bitwise and logical operator in an if statement in python

Below is the code, I want to the program to display something after verifying the variable status is between 0 to 20.下面是代码,我想让程序在验证变量状态在 0 到 20 之间后显示一些东西。

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

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. &and (你应该使用的那个)都期望两个表达式作为操作数,而<= 20不是一个有效的表达式。

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不被解析为嵌套表达式,如(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)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.语义几乎与0 <= status and status <= 20的语义相同,不同之处在于该status只评估一次。


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 .通常, x OP1 y OP2 z等价于x OP1 y and y OP2 z ,其中OP1OP2中的每一个都可以是><==!=>=<=isis notin ,或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.坚持“自然”的组合,如x < y <= zx < y == z等。

Try replacing & with and .尝试用and替换& In python, there is no && , there is only and .在 python 中,没有&& ,只有and In addition, when doing the and operator, each side of the and should be a valid comparison.另外,在做and运算符时, and的每一边都应该是一个有效的比较。 In the if statement, the first thing that is compared is status >= 0 , which returns a boolean.在 if 语句中,首先比较的是status >= 0 ,它返回 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.但是,在 if 语句的下一部分中,您输入了<= 20 ,它不会返回任何内容,因为它不是有效的比较。 Each part of the if statement should return a boolean value. if 语句的每个部分都应返回 boolean 值。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM