Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid syntax in "for item in L" loop [closed]

Tags:

python

I have a feeling I'm missing something pretty simple here but, in this one function:

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

I am getting the error:

    for item in L:
                 ^
SyntaxError: invalid syntax

For completeness my entire program looks like this:

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()

I am using Python 2.7.1. I have a semicolon after the for loop, the primes(n) function works, I have a feeling it is probably something stupid but I can't figure out what it is that is causing this invalid syntax.

like image 257
Dair Avatar asked Jan 16 '12 21:01

Dair


People also ask

How do I fix invalid syntax?

Defining and Calling Functions You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

What does invalid syntax Pyflakes E mean?

i wrote the above code,and the error was syntax error , it took me about an hour to figure out that my fullstop was outside the quotes. so in my own opinion, pyflakes E syntax error actually mean syntax error; you just have to check your code again.

Can I modify list while iterating?

The general rule of thumb is that you don't modify a collection/array/list while iterating over it. Use a secondary list to store the items you want to act upon and execute that logic in a loop after your initial loop.


1 Answers

You are missing a closing parenthesis on the line before:

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

See how we don't reach 0 in the "nesting count" below the line?

like image 127
unwind Avatar answered Nov 05 '22 07:11

unwind