Is_int


#1

Hi, I’m very stuck on the exercise “is_int”, could some one help me please?

Instructions:
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.

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.

For example:

is_int(7.0) # True
is_int(7.5) # False
is_int(-1) # True

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

My code:



def is_int(x):
  if x == int or x > 0:
    return True
  else:
    return False


#2

x is of type int, because x is an actual integer, while int is a datatype. We have the type() function to get the data type of a variable

also, how are you are going to deal with numbers which have 0 as decimal value? for example 7.0, type(7.0) gives float, not integer, any idea of how we could overcome this problem? Maybe check the hint?


#3

Hi, I’m sorry but I still dont understand.


#4

lets say x=2, then we get:

if 2 == int

2 does not equal int, so that won’t work. 2 (x) is of type int, so we can use this in our comparison.

I am not the right person to help you if you say i am still not able to understand, explain me where you are struggling, what you are thinking so i can help you fill in the gaps


#5

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