3/15 - It seems too simple


#1

My code:

Shorthand

def is_int(x):
    return x % 1 == 0

Long

def is_int(x):
    if x % 1 == 0:
       return True
    else:
       return False

Works fine, but I feel as if it was asking for something more complicated. What am I missing?

is_int
An integer is just a number without a decimal part (for instance, -17, 0, and 42 are all integers, but 98.6 is not).

For the purpose of this lesson, we'll also say that a number with a decimal part that is all 0s is also an integer, such as 7.0.

This means that, for this lesson, you can't just test the input to see if it's of type int.

If the difference between a number and that same number rounded down is greater than zero, what does that say about that particular number?


#3

Lets just complicate life seems more entertaining that way :grin:
code:

if type(x).__name__ == 'float':
    if x - float(int(x)) == 0:
        return True
    else:
        return False
elif type(x).__name__ == 'int':
    return True
else:
    return False

#4

I think that they're just getting you to think creatively about how to look at the problems that you may encounter. 7.0 is an integer the same as 7 even though they aren't officially the same type and if you encounter a situation where you are writing a code that wants integers, but not rounded numbers, this is a beneficial thing.

I was playing around with it and so mine ended up as:

def is_int(x):
    if float(x) - float(int(x)) == 0:
        return True
    else:
        return False

I was attempting to be more literal about their words (the difference between the number and it's integer form equals zero or something to that effect)

Edit --
Ironically I just went back and tested it and the function won't take floats as arguments. The course seems to not test any floats?


#5

Lots of different ways to do this one, here's what I used:

import math

def is_int(x):
    if x > math.floor(x):
        return False
    else:
        return True