Practice Problems: is_int How to fix the error?


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/isint?action=lesson_resume&link_content_target=interstitial_lesson

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

Instructions:

An integer is just a number without a decimal part (for instance, -17, 0, and 42 are all integers, but 98.6 is not).

For the purpose of this lesson, we’ll also say that a number with a decimal part that is all 0s is also an integer, such as 7.0.

This means that, for this lesson, you can’t just test the input to see if it’s of type int.

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

Define a function is_int that takes a number x as an input.

Have it return True if the number is an integer (as defined above) and False otherwise.

```python

Replace this line with your code.
def is_int(x):
if abs(x) != x:
if type(x) == int or type(x) == float or x - abs(x) >= 0:
return True
else:
return False

<do not remove the three backticks above>

#2

your condition:

if type(x) == int or type(x) == float or x - abs(x) >= 0:

why type(x) == float? A float is not a integer

if you want to do this, both values have to be absolute. then use int() for rouding down

its important to understand:

if 0 = 0.0:
    print True

that zero equals zero dot zero


#4

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