Is_int how to round a variable?


#1



In this exercise, we want to check if x is an integer. So my idea is to first create a variable y that is equal to x rounded. Example: x=5.6 => y=5.0
But up to now, I think I haven't seen a function that does that, so how am I supposed to do that?


def is_int(x):
    #I first want to create a variable y that is x rounded
    y=
    if x-y == 0:
        return True
    else:
        return False

Can someone tell me if I am trying to do the right thing here? And if maybe I missed that function in the previous lessons...
Thanks in advance :slight_smile:


#2

both abs() and int() have been taught at this point i think.

you can read about them in the python documentation:

abs
int


#3

Rounding would bring 5.6 to 6.0, not to 5.0. What you are thinking about is actually a floor, rather than a rounding. The floor of 5.6 is 5.0.

However, for the most relevant advice on the current exercise, see @stetim94's reply.


#4

My bad, indeed, int() has been taught at this point!

However, I don't see how to do that exercise with abs()
any hint? I'm just curious


#5

You're absolutely right @appylpye that's definitely not rounding. Thanks


#6

abs was taught here, and i provided a link to the python documentation (previous answer). See if that helps, otherwise we will help you further


#7

Thanks @stetim94
What I meant was that I managed to do the exercise with int(), but I didn't understand how abs() was useful for this exercise. So I was just curious if you had a solution to that exercise, but this time with the abs() (as you mentioned it).


#8

it is quit useful, you can do:

if abs(x) == int(x):

you get all integer values.

the absolute value equals the integer value for negative and decimal numbers with zero, but not for decimal numbers (except for zero)


#9

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