从分数分母的字符串列表中获取gcd

时间:2013-09-23 19:44:12

标签: python list greatest-common-divisor

这解决了它。

  

您将列表附加到dentemp([s [x]]),然后尝试在这些列表上应用int。 (也许你应该放弃额外的[]) - hcwhsa 19分钟前

我无法让我的列表成员能够用作数字来计算getgcd函数中的gcd。

我现在得到的错误:

Traceback (most recent call last):
  line 51, in <module>
    getgcd()
  line 42, in getgcd
    deninttemp=list(map(int,dentemp))
TypeError: int() argument must be a string or a number, not 'list'

代码:

from fractions import gcd
frac=[]
flipfrac=[]
numtemp=[]
dentemp=[]
deninttemp=[]
lntemp=[]
ldtemp=[]

def insert():
    print("Enter your Fractions, Enter 0 when done.\n")
    while True:
        x=str(input())
        if x!="0":
            frac.append(x)
        elif x=="0":
            print("done")
            break

def store():
    for i in range(len(frac)):
        l=frac[i].find("/")
        lntemp.append(l)
        s=frac[i]
        for x in range(l):
            numtemp.append([s[x]])

    for v in frac:
        flip=v[::-1]
        flipfrac.append(flip)


    for i in range(len(flipfrac)):
        l=flipfrac[i].find("/")
        ldtemp.append(l)
        s=flipfrac[i]
        for x in range(l):
            dentemp.append([s[x]])


def getgcd():
    for i in range (len(dentemp)-1):
        deninttemp=list(map(int,dentemp))
        a=deninttemp[i]
        b=deninttemp[i+1]


insert()
store()
getgcd()

0 个答案:

没有答案