所以我有這個功能。爲什麼我的功能不能打印
def test(fourBits):
binaryDigits = fourBits
if binaryDigits[1] == 1:
print 'test'
如果我進入test('1111')
它不會打印test
。我不明白爲什麼它不是?
所以我有這個功能。爲什麼我的功能不能打印
def test(fourBits):
binaryDigits = fourBits
if binaryDigits[1] == 1:
print 'test'
如果我進入test('1111')
它不會打印test
。我不明白爲什麼它不是?
使用此:
if int(binaryDigits[1]) == 1:
或該:
if binaryDigits[1] == '1':
使得類型匹配,即比較兩個字符或兩個數字。
嘗試print binaryDigits[1]
您if
語句之前看到什麼是被你的if
聲明隱藏。
在這裏沒有用。 ''1''和'1'打印完全一樣的東西。 – Cairnarvon
'print repr(...)'在這種情況下更有幫助 –
也許你想要的是這樣的。使用整數而不是字符串,並通過按位運算符測試該位。
def test(value):
if (value >> 1) & 1:
print 'true'
這是結果。
>>> test(0b0010)
true
>>> test(0b0000)
>>>
因爲'1111'是一個字符串,而1是一個整數。 –
也不需要在方法中設置局部變量。 – squiguy