Python “中的语法无效”;对于L";环

Python “中的语法无效”;对于L";环,python,Python,我有一种感觉,我在这里遗漏了一些非常简单的东西,但在这个函数中: def triplets(perimeter): triplets, n, a, b, c = 0 #number of triplets, a, b, c, sides of a triangle, n is used to calculate a triple L = primes(int(math.sqrt(perimeter)) #list of primes to divide the perimet

我有一种感觉,我在这里遗漏了一些非常简单的东西,但在这个函数中:

def triplets(perimeter):

    triplets, n, a, b, c = 0  #number of triplets, a, b, c, sides of a triangle, n is used to calculate a triple
    L = primes(int(math.sqrt(perimeter)) #list of primes to divide the perimeter

    for item in L: #iterate through the list of primes
        if perimeter % item == 0: #check if a prime divides the perimeter
            n = perimeter / item
            a = n**2 - (n+1)**2 #http://en.wikipedia.org/wiki/Pythagorean_triple
            b = 2n*(n+1)
            c = n**2 + n**2
            if a+b+c == perimeter: #check if it adds up to the perimeter of the triangle
                triplets = triplets + 1

    return triplets
我得到一个错误:

    for item in L:
                 ^
SyntaxError: invalid syntax
为了完整起见,我的整个程序如下所示:

import math

def primes(n): #get a list of primes below a number
    if n==2: return [2]
    elif n<2: return []
    s=range(3,n+1,2)
    mroot = n ** 0.5
    half=(n+1)/2-1
    i=0
    m=3
    while m <= mroot:
        if s[i]:
            j=(m*m-3)/2
            s[j]=0
            while j<half:
                s[j]=0
                j+=m
        i=i+1
        m=2*i+3
    return [2]+[x for x in s if x]

def triplets(perimeter):

    triplets, n, a, b, c = 0  #number of triplets, a, b, c, sides of a triangle, n is used to calculate a triple
    L = primes(int(math.sqrt(perimeter)) #list of primes to divide the perimeter

    for item in L: #iterate through the list of primes
        if perimeter % item == 0: #check if a prime divides the perimeter
            n = perimeter / item
            a = n**2 - (n+1)**2 #http://en.wikipedia.org/wiki/Pythagorean_triple
            b = 2n*(n+1)
            c = n**2 + n**2
            if a+b+c == perimeter: #check if it adds up to the perimeter of the triangle
                triplets = triplets + 1

    return triplets

def solve():
    best = 0
    perimeter = 0
    for i in range(1, 1000):
        if triplets(i) > best:
            best = triplets(i)
            perimeter = i
    return perimeter

print solve()
导入数学
def primes(n):#获取数字下方的primes列表
如果n==2:返回[2]

elif n您在以下行之前缺少一个右括号:

      L = primes(int(math.sqrt(perimeter)) #list of primes to divide the perimeter
#               ^   ^         ^         ^^
#nesting count  1   2         3         21

查看如何在该行下方的“嵌套计数”中不达到0?

该行前面有一个错误:

L = primes(int(math.sqrt(perimeter))

您有3个打开的括号,但只有两个关闭的括号。

错误在上面一行-您缺少一个关闭括号:

L = primes(int(math.sqrt(perimeter)))

您缺少一个括号:

L = primes(int(math.sqrt(perimeter)))
                                    ^
                                    |
                                 this one

这种情况经常发生在我身上,你只需要看看前面的线路就可以了。

shot。我知道这很简单。太尴尬了。哈哈。谢谢。我想评论一下:我犯了另外两个错误(我后来发现):
b=2n*(n+1)
应该是
b=2*n*(n+1)
此外,我还需要以不同的方式初始化三元组,a、b、c和n。
L=…
行中的括号