I’m having a hard time understanding why I can’t assign a return value from this simple function to the variable gcd:
def euclidAlgorithm(m, n):
if n == 0:
print "n cannot be zero."
return -1
r = m % n # Remainder
if r == 0:
return n
else:
euclidAlgorithm(n, r)
if __name__ == "__main__":
#Input values
m = 54
n = 36
print "Input : m = ", m, " n = ", n
gcd = euclidAlgorithm(m, n)
if gcd == -1:
print "Function terminated with an error"
else:
print "Output: gcd = ", gcd
Instead of getting gcd as 18 I get this when I run it:
Input : m = 119 n = 4
Output: gcd = None
You’re missing a
returnstatement at the end of youreuclidAlgorithm(m, n). Like this: