The Man Behind The Mask


#1



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


I keep getting the message:
"Oops, try again. Does your check_bit4 function take exactly one argument (an integer)? Your code threw a "unsupported operand type(s) for &: 'str' and 'int'" error. "


It should check to see if the fourth bit from the right of the input variable is on then return on if it is, or off if it is not.
How do I get rid of the error message and pass the test?


This is what I coded...

def check_bit4(input):
    input = int(input)
    input = bin(input)
    mask = 0b1000
    desired = input & mask
    if desired > 0:
        return "on"
    else:
        return "off"

What am I doing wrong?


#2

bin() returns string (string of 1's and 0's) and not a binary number actually. also input is already an int so casting using int() constructor seems useless.

hint*
You can delete that line of code?


#3

Thanks for explaining lines I can remove.
Any advice on lines I can add?


#4

Is code working or It has still having the bug ?


#5

True.
Thought there was something I hadnt coded into the algorithm.
Thanks. But if I may ask what exactly does the input keyword do?


#6

input is not a keyword in python but built-in function (precisely it's input() ) so we are able to create variable name(identifier) using that.
here it's a variable name, that is of integer type.
keywords in python can't used as identifier(object names).


#7

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