类型错误:& 不支持的操作数类型:“str”和“int”

Mad*_*ari 1 typeerror python-2.7

我在 python 2.7 上执行以下代码段:

i=0
j=3
a=['A','B','B','A']
while(a[i]=="A" & i<j):
    #do something
Run Code Online (Sandbox Code Playgroud)

我收到这个错误。

类型错误:& 不支持的操作数类型:“str”和“int”

有什么帮助吗?

ᴀʀᴍ*_*ᴍᴀɴ 5

&是 Python 中的“按位和”操作数,您应该and改用

来自 wiki.python.org:

x & y :执行“按位与”。如果 y 的 x AND 的对应位为 1,则输出的每一位为 1,否则为 0。

“按位与”的工作方式如下:

>>> 1 & 0
0
>>> 0 & 0
0
>>> 1 & 1
1
Run Code Online (Sandbox Code Playgroud)