Python:gcd代码未打印

Python:gcd代码未打印,python,greatest-common-divisor,Python,Greatest Common Divisor,我编写了一个代码来计算用户输入的数字的最大公分母 但是我不知道为什么这个代码没有打印结果。 我想,可能是因为while循环需要太多的时间来计算 如果我输入第一个数字=4,第二个数字=6,则不会打印结果 但如果我输入First number=12和second number=24,结果将打印为“12” 修复此代码的更好方法是什么 num1 = int(input("Enter the fist number: ")) num2 = int(input("Enter the second numbe

我编写了一个代码来计算用户输入的数字的最大公分母

但是我不知道为什么这个代码没有打印结果。 我想,可能是因为while循环需要太多的时间来计算

如果我输入第一个数字=4,第二个数字=6,则不会打印结果

但如果我输入First number=12和second number=24,结果将打印为“12”

修复此代码的更好方法是什么

num1 = int(input("Enter the fist number: "))
num2 = int(input("Enter the second number: "))

def gcd(num1, num2):
    i = min(num1, num2)
    while True:
        if (num1 % i == 0) and (num2 % i == 0):
            return i
        i - 1

print("The greatest common denominator of both number is", gcd(num1, num2))

python中不存在用于减少i的符号,应将其更改为:

i=i-1

您没有更改i的值。只需使用
i=i-1
而不仅仅是i-1


这就是为什么当你的数字在第一次迭代中没有除以较大的数字时,它会进入无限循环,因为你没有改变i的值

首先,“
i-1
不会改变
i
。。。。尝试
i-=1