def is_multiple(m, n):
"""
>>> is_multiple(12, 3)
True
>>> is_multiple(12, 4)
True
>>> is_multiple(12, 5)
False
>>> is_multiple(12, 6)
True
>>> is_multiple(12, 7)
False
"""
c = m / n
d = int(m / n)
if (c-d)== 0:
return True
elif (c-d)!= 0:
return False
I am trying to make a function that decides if m is a multiple of n. I can only use what I have learned which is functions, if/else, if/elif/else statements, Boolean expressions and simple things like that. I have not gotten to for, while, do/while expressions yet. My code keeps returning True for everything which it is false in two cases. So why does it keep returning True, and also is there a more simpler way to write a function for finding a multiple of a number?
Updated code:
def is_factor(f,n):
"""
>>> is_factor(3, 12)
True
>>> is_factor(5, 12)
False
>>> is_factor(7, 14)
True
>>> is_factor(2, 14)
True
>>> is_factor(7, 15)
False
"""
if n % f == 0:
return True
else:
return False
So that code can be used to find a multiple of a number also?
Correct updated code that produces correct result:
def is_multiple(m, n):
"""
>>> is_multiple(12, 3)
True
>>> is_multiple(12, 4)
True
>>> is_multiple(12, 5)
False
>>> is_multiple(12, 6)
True
>>> is_multiple(12, 7)
False
"""
c = float (m) / n
d = m / n
if (c-d)== 0:
return True
else:
return False