Finding a multiple function keeps returning true and is there a simpler way to write this function in python -


this question has answer here:

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 trying make function decides if m multiple of n. can use have learned functions, if/else, if/elif/else statements, boolean expressions , simple things that. have not gotten for, while, do/while expressions yet. code keeps returning true false in 2 cases. why keep returning true, , there more simpler way write function finding multiple of 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 code can used find multiple of number also?

correct updated code 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 

your is_factor looks correct. simplify to

def is_factor(f, n):     return n % f == 0 

if want check if x multiple of y, same saying y factor of x. thus, can do

def is_multiple(m, n):     return is_factor(n, m) 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -