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
    else:
        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.

2 Likes

Thank you so much

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

4 Likes

You’re welcome :slight_smile:

from fractions import gcd

print gcd(num1,num2)

will do the job.

//