-1

Hello guys i need to write a programm which calculates the greatest common divisor of two numbers. The programm should throw an exception when 1 of the 2 input numbers are zero. But my code always throws this Exception now :( Hope you guys can help me :)

def ggt (a,b):
    if (b) or (a)==0:
        raise Exception(ValueError)
    else:
        return ggt(int(b), int(a%b))

print (ggt(25,14)

1 Answers1

0

If I understand the question correctly, it can be done like this:

def ggt(a, b, first_time=True):
    if a * b == 0 and first_time:
        raise Exception(ValueError)  # the error occurs only if 0 on the first iteration
    elif b == 0:
        return a
    else:
        return ggt(b, a % b, False)
Алексей Р
  • 7,507
  • 2
  • 7
  • 18