Help needed!


#1

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

def is_int(n):
if n is int:
return True;
elif abs(n)==int:
return True;
elif n==float:
if (n-int(n))<=0:
return True;
else:
return False;


#2

A number isn't going to be equal to a type, that's like saying "is this apple equal to this orange?" - no, they're not even the same type of fruit. You'll need to think about how an integer behaves differently from other numbers and test that behaviour.


#3

ohh really,
I need to compare with type of n!!
thanks...


#4

The exercise asks you to distinguish between 1.0 and 1.2, they have the same type so you can't use that at all.
Instead you'll need to think about what it means to have decimals, for example, removing the integer part would cause an integer to have 0 remaining while 1.2 would have 0.2 remaining. You can also look at what happens when you divide by 1, what's going to happen with the remainder when you do that for 1.0 and 1.2?


#5

for float does this code solve the problem you have stated!!

if (n-int(n))<=0:
return True;

explaining the code:
for 1.0 :
1.0-1==0 and it returns true
where as for 1.2
1.2-1>0 hence it returns false:


#6

What about -5.5?


#7

for that i need to convert again!!!
hmmm..
if abs(n)-int(abs(n)<=0:
print True;


#8

Why does -5.5 test as an integer with your function?
-5.5-(-5) -> -0.5 this is less than 0, would an integer ever give a result of anything other than 0?


#9

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