The man behind the mask


#1

The result of this program is correct, that codecademy said. But why there is no message of result in output.

def check_bit4(input):
checkBit = 0b1000
checker = checkBit & input
if checker > 0:
return 'on’
else:
return 'off’
number = 0b1101
print check_bit4(number)


#2

I have just understand. The problem was in indentation


#3

Extra study

Python has some beautiful syntax (ternary) for this type of problem:

>>> def check_bit4(number):
	return 'on' if number & 0b1000 else 'off'

>>> check_bit4(8)
'on'
>>> check_bit4(10)
'on'
>>> check_bit4(24)
'on'
>>> check_bit4(33)
'off'
>>> 

Also, we should not make a practice of using reserved words as variables. Use in a parameter does not cause any problems (because of local scope) fortunately, but it is still not a good practice.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.