2016-01-11 53 views
-1

我执行Python的2.7下面的代码片段:类型错误:不支持的操作类型为:“海峡”和“廉政”

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

而且我得到这个错误。

TypeError: unsupported operand type(s) for &: 'str' and 'int'

任何帮助?

+0

[类型错误:不支持的操作数类型(一个或多个),用于: - 'INT' 和 'STR']的可能的复制(http://stackoverflow.com/questions/13831905/typeerror-unsupported-operand-types-for-int-and-str) –

+0

@JarrodRoberson:它可能看起来很相似,但操作符是不同的,如果你看到的话。我不知道这个解决方案对于不同的运营商来说是相似的。 – madhavi

回答

1

&是 “按位与” 操作数在Python中,你应该使用and代替

从wiki.python.org:

x & y : Does a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0.

“按位和” 是这样的:

>>> 1 & 0 
0 
>>> 0 & 0 
0 
>>> 1 & 1 
1 
+0

可能的答案是:“类似的问题”不能解决我的问题。你如何建议我解决这个错误? – madhavi

相关问题