My solution for 3 / 15


def is_int(x):
if x == int(x):
return True
if x % 2 == 0 or x % 2 == .5:
return True
return False

print is_int(-2)
print is_int(5.0)

Note from moderator: please refrain from posting code answers as doing so is against the forum guidelines. Thanks!


Ok let's do this and you will see you have an issue,


Does this

# ValueError: invalid literal for int() with base 10: 'a'

Now I do not blame you, but they did a terrible job asking us to define a function that determines if something is an integer.

In order to properly do this you need to use try as follows

def is_int(data):
        return int(data)
    except ValueError:
        print("You failed to enter a valid entry!")

Now this does not tell us if it is explicitly an int type, this will

def is_int(data):
    This Function returns the data if it is a whole number positive/negative or zero
        return int(data) if data % 1 == 0 else False
    except TypeError:
        print(False, "error")


I agree with zeziba that in a real world application we’d need to sanitize input but for the purposes of this course I think it’s overkill.

You should check your function as it returns True for is_int(2.5)

The hint for this exercise is super useful and points you to solving the problem with a single line.


I had some extra stuff in there on accident from my other code. I just copy and pasted it lol. It's the right code now.