3 is_int


#1


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

it says Oops, try again. Your function fails on is_int(-2). It returns True when it should return True.

it is returning true when it has to do so!!!!!!!! why is it not allowing me to proceed ?? help me out please


def is_int(x):
    if x - int(x) > 0:
        return"False"
    else:
        return"True"


#2

Hi @ravurisushmasree,

Your code is on the right track! You did a fine job of thinking through the problem. A couple things:

  • You're returning a string containing "True" or "False" instead of returning the actual Boolean value.

A bool can be either True or False, and there is no need for quotation marks.

What you're looking for is:
return True and return False

  • Also, think about the definition of an int. It can be any whole number, positive or negative. You're testing the number to see if the difference between the int and the float is a positive number, however; what if it is negative? For example, -3.4 rounds to -3 as an int. Therefore, the difference would be -3.4 - (-3), which equals -0.4. This should return False, but your code returns True since the value is less than 0.

What you can do is use a different operator in your conditional statement. Instead of > 0, try != 0

This tests for positive and negative remainders when you do your subtraction.

Hope this helps!


#3

hey @wowpalvabbit!!
thanks for your help..i even tried the way you explained of getting along with != thing..but because of returning string i cudnt find the error and changed the expression the present way!!
anyway! once again thankz a ton for hepin me :smile:


#4

@wowpalvabbit Yoour explanation is just great!!! :grinning:


#5

Or just:

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

#6

def is_int(x):
if x -int(x) ==0:
return True
else:
return False
hope this works for u...:slight_smile:


#7

I keep on getting the error message : "Oops, try again. Your function fails on is_int(-3.4). It returns True when it should return False."
My code looks like this:
def is_int(x):
if x - int(x) > 0:
return False
else:
return True

What am I doing wrong?
I do have the correct indents.


#8

change the '>' to '!='

this works for me:
def is_int(x):
if x - int(x) != 0:
return False
else:
return True


#9

Thank you for the help


#10

hi zeeshan here!
1. try !=0
2. Please remove double quotes of "False" and "True"
just try this code
def is_int(x):
if x - int(x) != 0:
return False
else:
return True


#11

def is_int(x):
if x > 0:
if x - int(x) > 0:
return False
elif x - int(x) == 0:
return True

elif x < 0:
    if (-x)- int(-x) > 0:
        return False
    elif (-x) - int(-x) == 0:
        return True
else:
    return True

#14

if x%1==0:
return True
else:
return False


#15

Brilliant, yet simple.


#16

def is_int (x):
if x-round(x,0)==0:
return True
else:
return False


#17

#18

#20