13. is_int (modulo confusion)


#1



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

After searching the forums, I am given to understand that the code below is a valid solution for this exercise, but I don't understand why. To my understanding, a number divided by 1 always leaves a remainder of zero. Why then would modulo not return a remainder of zero for a number with a decimal point?


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


#2

You typed define instead of def

Your code:
define is_int(x):

Should be:
def is_int(x)


#3

Thanks for your response, however I'm not looking for an issue with my syntax. I catch those after submitting code.

Do you have any insights regarding my question about modulo?


#4

well, for example 3.5 % 1, i have 0.5 left after division. The remainder is what is left after division, this include the decimal numbers


#5

You'll have to forgive me, as it's been 20 years or so since I've bothered with this kind of math, so I may be missing the obvious here.

But even on a calculator, 3.5 divided by 1 equals 3.5, with a remainder of zero.

Or as I tend to convert these things into English in my head: 3.5 divided equally into 1 group is 3.5.

This is where I'm getting confused...


#6

but 3.5 % 1, means the biggest possible division which can be made is 3 (3*1), 4 is no longer possible (4*1), so to calculate the remainder i have to number minus biggest possible division (3.5 - 3), which means i have 0.5 remaining


#7

Modulo is more strict than division. Its purpose is to find the remainder, and its purpose it shall fulfill :wink:

To a computer, the expression 3.5 % 1 means to divide 3.5 by 1 and return any remainder. Well, 3.5 divided by 1 is literally 3 with a remainder of 0.5. Humans write that as 3.5, but the modulo operator returns only the 0.5.


#8

Thanks. It's been a very long time since I had any cause to think about remainders in long division. It came back to me once I got out a pad of paper and did it by hand. :slight_smile:


#9

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