Trivial python problem

So, I was writing a code for calculating GCD using the Euclidean theorem.
Here is my code:
link text

Can anyone please explain to me that why is it returning None, though ‘a’ is still storing the correct value? I just can’t figure out what’s happening wrong.

The function isn’t returning a value by default. Add a ‘return’ before gcd(b, a1) and it works.

1 Like

Your syntax for the recursive program is wrong. Your code should look like this.

def gcd(a, b):
    print a, b
    if b == 0:		
        return a
        return gcd(b, a%b)

inp = int(raw_input())
inp1 = int(raw_input())
a= gcd(inp, inp1)
print a

Furthermore, your program was giving output as None because your program was not returning anything if b is not equal to 0. That means it was returning None.


The last line of the recursive gcd function should be “return gcd(b,a1)”.


from fractions import gcd

print gcd(num1,num2)

will do the job.

Thank you so much

You’re welcome :slight_smile: