11. The man behind the mask: Right output but programm says my code is wrong


#1

def check_bit4(input):
newIn = input >> 3
if newIn == 1:
print "on"
else:
print "off"

check_bit4(0b0)

The console returns "off" and the environment throws the error:

Oops, try again.
Your function fails on check_bit4(0b0). It returns "None" when it should return "off".

What did I do wrong, how to get the program to "see" that my code is right?


#2

replace the print statements with return


#3

thank you, that was the problem


#4

Ha! That's quite a funny way of tackling the question! Kudos for thinking outside the box :smile: You're not masking here though, so the solution won't work if you're asked to check more than one bit. In that case you'd be back to masking:

def check_bits4_2(input):
    if ((input & 0b1010) > 0):
        return "on"
    else:
        return "off"

#5

def check_bit4(inout):
if inout >>3 >0:
return "on"

else:
    return "off"

print check_bit4(0b01000)


#6

Here is the code I used that works

def check_bit4(input):
mask = 0b1000
test = input & mask
if test > 0:
return "on"
else:
return "off"
print check_bit4