2012-01-16 32 views
7

mam wrażenie, że jestem brakuje czegoś tu całkiem proste, ale w tej jednej funkcji:Nieprawidłowa składnia w „dla elementu L” pętli

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 

otrzymuję błąd:

for item in L: 
       ^
SyntaxError: invalid syntax 

kompletności mojego całego programu wygląda następująco:

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

Używam Python 2.7.1. Mam średnik po pętli for, funkcja primes(n) działa, mam wrażenie, że jest to prawdopodobnie coś głupiego, ale nie mogę zrozumieć, co to jest przyczyną tej nieprawidłowej składni.

+0

nawias w linii 'L = ...' –

Odpowiedz

13

Brakuje nawias zamykający na linii przed:

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

Zobacz, jak nie osiągnie 0 w „zagnieżdżenia count” pod kreską?

+0

Strzelaj. Wiedziałem, że to coś prostego. Bardzo zawstydzony. Ha ha. Dzięki. – Dair

+0

Chciałbym tylko dodać komentarz: Zrobiłem dwa inne błędy (które odkryłem później): 'b = 2n * (n + 1)' powinno być 'b = 2 * n * (n + 1)' również, potrzebowałem w celu inicjowania tripletów, a, b, c oraz n w inny sposób. – Dair

0

Wystąpił błąd w linii przed nim:

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

Masz 3 parens otwarte, ale tylko dwóch nawiasów zamykających.

0

Błąd znajduje się w wierszu powyżej - tracisz bliską Wspornik:

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

Brakuje nawias:

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

Dzieje się tak do mnie wszystkie czasy, ty wystarczy spojrzeć na linię wcześniej.

Powiązane problemy