FAQ: Learn Python - Practice Makes Perfect - is_int

This community-built FAQ covers the “is_int” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise is_int:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Why is the ‘True’ being printed to the console when I’m not asking the function to print it, but only return it?
Here’s my code:

def is_int(x):
absolute = abs(x)
rounded = round(absolute)
result = absolute - rounded
if result == 0:
print “This number is an integer.”
return True
else:
print “This number is not an integer.”
return False

print is_int(10.0)

The indentations are not visible when I post this. I’m not sure why.

1 Like

“print is_int(10.0)”

You asked the console to print the return statement.

They will be preserved if you make use of a code box, accessible via the </> icon that you will see in the middle of the menu bar that appears when you begin to type in a text box.

1 Like

def is_int(x):
absolute = abs(x)
rounded = round(absolute)
return absolute - rounded == 0

How does this return a True or False?
In the code there is no statement to return True or return False for any condition, then how does it print such?
Only thing being returned in the difference of the ‘absolute’ and ‘rounded’ number being equal to zero.
Bit confused

1 Like

Why can’t we use the type() function?

This exercise is treating any whole float as an integer, so 7.0 (which is a float) is considered an integer in this exercise. They should have worded the question a little better, maybe instead asking if the number provided was a whole number, instead of asking if it was an integer.

When you ask the compiler to return a boolean operation (such as rounded==abs), you are creating a boolean value, it is a great way to shorten the code required for simple operations.

if rounded==absolute:
return True
else:
return False

can be shortened to :
return rounded==absolute

because if rounded does equal absolute then the operation will be found True, and vice versa

1 Like

Don’t know if it’s an elegant way, but this worked for me:

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

print is_int(-3.6)```