Python项目Euler#57,了解

2024-05-15 17:56:16 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行此代码来计算根2连分式的收敛性时:

def root_two_expansion():
    total=0
    a=(3,2)
    b=(7,5)
    x=3
    while x<=1000:
        c=((sum(a)+sum(b)),sum(b))
        if len(str(c[0]))>len(str(c[1])):
            total+=1
        a=b,b=c
        x+=1
    return total

print root_two_expansion()

我收到这个错误:

069.506.py", line 7, in root_two_expansion c=((sum(a)+sum(b)),sum(b)) TypeError: 'int' object is not iterable

为什么会这样?我不知道代码在哪个阶段尝试对int进行迭代。如果有任何建议,我将不胜感激。你知道吗


Tags: 代码lenreturnifdefrootinttotal