Advice about my code


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/3?curriculum_id=4f89dab3d788890003000096

my code works

but i want to know if the logic behind it is correct, i didnt think about the round() function , should I be using that instead? thanks in advance.

def is_int(x):
    if x == 0:
        return True
    elif x / int(x) == 1:
        return True
    else:
        return False


#2

I tried your code, and I got the same answer as my code did:

def is_int(x):
    if x - round(x) > 0:
        return False
    else:
        if x < 0 and x - round(x) < 0:
            return False
        else:
            return True

So I guess you did it right, you just used a different way to reach your goal. :grin:
Sorry In advance for the bad response...I'm new at helping people :grinning:


#3

Your logic is correct but naturally there is more than one way to determine if a number is an integer (in general, not just in python).

Your approach works, but required the exception for 0. You could have done something like
if X!=int(X):
return False

else:
return True

or something like

if X>int(x) or X<int(x):
return false

else:
return True

Or like you mentioned the round function. If you use the round function on X and that still is equal to X return True. All are valid ways of checking for integers.


#4

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