我執行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