Is_int! I not understand the bug


#1



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


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


I not understand. I try to think to the entire stack of the function but i can't find the bug.
With negative number


y=abs(float(raw_input('y =')))
print y

def is_int(y):
    if y - abs(int(y)) > 0:
        print ('not integer')
        return False
    else:
        print ('is a integer')
        return True
        
is_int(y)


#3

Your is_int(y) function works only with positive numbers. If user inputs negative number, you make it positive and then give it as argument for the function. But I think codeacademy program doesn't do so.
For example if number is -3.4:
your program do this:
y=abs(float(raw_input('y ='))) -> y = 3.4
def is_int(3.4):
if y - abs(int(y)) > 0: -> 3.4 - 3 > 0 -> '' -3.4 not integer" (It's OK)
but if:
def is_int(-3.4):
if y - abs(int(y)) > 0: -> -3.4 - 3 > 0 -> -6.4 > 0 -> "is a integer" (Wrong)
I think you need remove first two strings and change condition of if statement.


#5

The idea is to work without using the built-in function. How might we determine if the number has a decimal fraction component? The Hint gives us one approach,

n % 1

will be 0 if there is no decimal fraction. We are told that 7.0 is to be treated as an integer.


#6

Yeah i understand your suggest.
I resolved before to see your reply and probably i used oversize memory :slight_smile:

y=float((raw_input('y =')))
print y



def is_int(y):
    if y > 0:
        if y - int(y) == float(0):
            print ('integer')
            print ('true')
            return True
        else:
            print ('not integer')
            return False
    elif y < 0:
        if y + int(abs(y)) == float(0):
            print ('integer')
            print ('true')
            return True
        else:
            print('not integer')
            return False
    elif y == 0:
        print ('integer')
        return True

        
is_int(y)

#7

Now see if you can find one or two simpler methods that do not use int() or float().


#9

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