3. is_int


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/3?curriculum_id=4f89dab3d788890003000096


I'm not sure what to think here. I didn't think this code would be correct and I'm almost certain it's incorrect yet the console passed it as correct. After reading through the exercise text did I do what it says you can't do? I really have to stop doing this when it's late and I'm tired.


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


#2

your code is absolutely correct.you aren't checking whether it's integer or not,you make the variable integer.


#3

It's correct? It isn't wrong? The code, from my understanding, is just checking if x is the same as the integer version of x so if x is 4.4 ...... oh, I'm comparing 4.4 to see if it's the same as 4.

I don't know how I stumbled into that when it feels so wrong. I searched and found nothing like my solution and the most common seems to be the following since any number leaving a remainder isn't an integer.

def is_int(x):
    if x % 1 == 0:
        return True
    else:
        return False

#4

It's too correct.
what does the instruction want?

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

from lesson:

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.

so simply it'll only be True if the decimal part is 0 that's what we want.isn't it?

also there are a lot of way to achieve this.


#5

hello! Is any code possible in these exercise with the given definition that is of carried out difference .I have tried one :
def is_int(x):
if abs(x)-(abs(x/2)*2)==0:
return Trbue
else:
return False
is_int(5.5)


#6

I could be tripping (I have a tendency to make simple errors), but I'm pretty sure that regardless of what float or integer you set your variable to

if abs(x) - (abs(x/2) * 2) == 0:
     return True

will literally always return true.

Again, I could be tripping, but I think the absolute value of x (for floats and integers) will always be equal to abs(x/2) * 2, so those terms would always cancel each other out.


#8

import math

def is_int(x):
if abs(x) -abs(math.floor(x)) == 0:
return True
else:
return False
absolute value of x - absolute value of the number before the , what you think?


#9

import math
def is_int(x):
    if math.fabs(x)-math.fabs(int(x))>0:
        return False
    else:
        return True

#10

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