2016-01-11 110 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

相關問題