我执行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'
任何帮助?
我执行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'
任何帮助?
&
是 “按位与” 操作数在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
可能的答案是:“类似的问题”不能解决我的问题。你如何建议我解决这个错误? – madhavi
你需要围绕如下两个条件放置括号。
i=0
j=3
a=['A','B','B','A']
while((a[i]=="A") & (i<j)):
#do something
参阅下面链接,更详细的解释 Difference between 'and' (boolean) vs. '&' (bitwise) in python. Why difference in behavior with lists vs numpy arrays?
你是否真的认为他们想在这种情况下使用按位操作? – JJJ
[类型错误:不支持的操作数类型(一个或多个),用于: - 'INT' 和 'STR']的可能的复制(http://stackoverflow.com/questions/13831905/typeerror-unsupported-operand-types-for-int-and-str) –
@JarrodRoberson:它可能看起来很相似,但操作符是不同的,如果你看到的话。我不知道这个解决方案对于不同的运营商来说是相似的。 – madhavi