Search code examples
pythonfractionsgreatest-common-divisor

Python GCD - errors


I have an issue with my code whose purpose is to find the GCD of two inputs. When I try to run the module it tells me that 'gcd' is not defined.

def GCD(12,4):
    gcd = 1
    for i in range(2, max(12,4)/2):
        if((12 % i == 0) and (4 % i == 0)):
            gcd = i
    return gcd

Solution

  • You are not calling the GCF function. You have just defined your function. You need to add a line

    gcf = GCF(a,b)
    

    after the place where you accept the input. That is after b = int(input('denomenator: '))

    Edit:

    Change the input statements to

    a = float(input('numerator: '))
    b = float(input('denomenator: '))