Is this broken?


Whenever I try to run this I get the following message: "Your function fails on flip_bit('0b111', 2). It returns "None" when it should return "0b101"."

However, when I call "flip_bit('0b111', 2)" I get this message:

Traceback (most recent call last):
File "python", line 6, in
File "python", line 3, in flip_bit
TypeError: unsupported operand type(s) for ^: 'str' and 'int'

When I run "flip_bit(0b11111,2)" , it works fine and outputs "0b11101".
It seems like the problem is that it's trying to input a string when it needs to be a binary number. Is this the case, or am I doing something wrong?

def flip_bit(number,n):
    mask = 0b1 << n - 1
    result = number ^ mask
    print bin(result)
flip_bit('0b111', 2)


I think you need to return the result. Not print it.
As per what the 3rd instruction says:

03. Return the result of calling bin(result).

Also, your input should be like this:


#not flip_bit('0b111', 2)


Well, I feel stupid now.


Haha, everyone has those monents xD


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