Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python GCD-错误_Python_Fractions_Greatest Common Divisor - Fatal编程技术网

Python GCD-错误

Python GCD-错误,python,fractions,greatest-common-divisor,Python,Fractions,Greatest Common Divisor,我的代码有一个问题,其目的是查找两个输入的GCD。当我尝试运行模块时,它告诉我没有定义“gcd” 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 您没有调用GCF函数。您刚刚定义了您的函数。您需要添加一行 gcf = GCF(a,b) 在您接受输入的位置之后。

我的代码有一个问题,其目的是查找两个输入的GCD。当我尝试运行模块时,它告诉我没有定义“gcd”

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

您没有调用
GCF
函数。您刚刚定义了您的函数。您需要添加一行

gcf = GCF(a,b)
在您接受输入的位置之后。在
b=int(输入('denomenator:'))之后

编辑:

将输入语句更改为

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

您可以使用欧几里德除法算法在更短的时间内找到gcd。 将浮点数取为a和b

def gcd(a,b):
    c = 1
    a,b = max(a,b),min(a,b)
    while c != 0:
        c = a%b
        a,b = b,c
    return a
print gcd(12,5)

好的,我这样做了,但现在它说“TypeError:‘float’对象不能解释为整数”,正如您所知,Python附带了分数模块。包括