Practice Makes Perfect 3/15 Can you explain the code?

#1

It seems that I totally didn’t get what they were asking for in this part…

The way I interpret what they are askin us is - Is the number we call x with an int or a float, IF it is int then that is True and ELSE(float) then that is False…

This is my code(and it actually worked), but the solution is waaaaay different:

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

And the Codecademy solution:

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

Can somebody explain what they are actually asking for and explain their solution to me?

And let me take it a step further and call x with the number 7.5, the code will look like this. But can you explain what I am looking at here…?

def is_int(x): #7.5
absolute = abs(x) #7.5
rounded = round(absolute) #8.0
return absolute - rounded == 0 #7.5 – 8.0 which is equal to -0,5 so -0.5 == 0

I see that -0.5 is a float and is not the same as 0 and that will be interpreted as False I guess, but still…whats the deal whit that code…? They want us to find out if the number is an int or a float NOT based on the number it self, but on the differnce between the number and the number when its rounded…? Im I on the right track?

If so I would never in a million years have guessed that thats how Im supposed to write the code…

#2

The return value is a boolean. That is a comparison, not an assignment.

There are any number of ways to determine if a number is an integer, and that is just one of them. The instructions specifically stated to NOT use `int()`, as I recall.

Not sure if `abs()` or `round()` were even covered up to this point so clearly they expect us to do some background research before embarking upon the problem.

You have a fairly good notion of what that solution is doing, though. `round()` will round up on .5 and down on .4, but in any event, if there is a difference then the number is not an integer.

Technically it is not necessary to cast the absolute value. Any difference will be either zero, or non-zero. That’s the tell.

``return x - round(x) == 0``

#3

RETURN is actually telling us True or False?

And in my example False because -0.5 is not the same as 0?

#4

Not `return`, but the expression it is returning, which is evaluated first, then returned.

Correct. If the difference is not zero, then the number is not an integer.

#5

They mentioned abs() in the earlier lessons, but not round(), but it was easy to see what it was doing when I you look at the code, but I still think they should have mentioned it so that people can say “Hey what if we use this method or that method”, its hard to do research on something that you dont know even exist, unless you look at the solution

Yes, and I got that part too, if the difference is not .0 then you have a float…

Ok so it basically returns either True or False, without the need to show it to us on the screen, and without us needing to actually write a print statement with True or False inside an If/else statement…?

#6

Correct. Rarely do we ever need to use literals in our code since boolean expressions result in one or the other.

``````def is_42(x)
return x == 42

is_42(42)     => True
is_42('42')   => False
``````

As for other solutions, here is another one that you may be familiar with…

``return x % 1 == 0``

#7

Thanks for the help again!

#8

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