m = int(input("First number (0 to stop): "))
n = int(input("Second number: "))
def gcd(a, b):
while b != 0:
c = a % b
a = b
b = c
if b == 0:
break
return a
print ("The greatest common divisor of", n,"and", m, "is", abs(gcd(m,n)))
当m等于0时,如何摆脱此while循环。
答案 0 :(得分:2)
您可能想要一个外部循环,从您的(0 to stop)
输入提示判断:
def gcd(a, b):
while b != 0:
c = a % b
a, b = b, c # tuple assignment FTW!
if b == 0:
break
return a
while True:
m = int(input("First number (0 to stop): "))
if m == 0:
break
n = int(input("Second number: "))
print("The greatest common divisor of {0} and {1} is {2}".format(n, m, abs(gcd(m, n))))