Is this broken?


#1



https://www.codecademy.com/courses/python-intermediate-en-KE1UJ/2/4?curriculum_id=4f89dab3d788890003000096#


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(0b11111,2) 
flip_bit('0b111', 2)


#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:

flip_bit(0b11111,2) 

#not flip_bit('0b111', 2)

#3

Well, I feel stupid now.


#4

Haha, everyone has those monents xD


#5

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