Python having trouble with this problem why doesn't it work


#1

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

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


#2

-2 - -2 = 0
Thus it returns false, you only allow things greater than 0 to be true in your method


#3

@fight_dragons is correct.

Look at intent of the function again. Re-read the instructions. You are building a function that checks if a number is an integer. And for this exercise, 7.0 and 7 are both integers. But 98.6 is not as it’s a floating point number.

They give you a clue in the description to help you craft the check:

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

Think about what the above sentence and clue is saying to you. If we compare the number passed in to the same number rounded, then we’d know if it’s an integer. How? Because it would equal.

Try this to see what I mean:

print rounded(7.0) //-> 7.0 that does == 7.0
print rounded(7.1) //-> 7.0 AHA that doesn't == 7.1

Think about it.


#4

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