syntax error in sum(). Please explicate.

Matt Moriarity matt.moriarity at gmail.com
Sat Nov 18 23:33:16 CET 2006


try surrounding your sum argument in brackets:

sum([phi(x // ps[i+1], i) for i in range(a)])

instead of:

sum(phi(x // ps[i+1], i) for i in range(a))

On Nov 18, 5:23 pm, Michael Press <j... at abc.net> wrote:
> I have not written python codes nor run any. I saw this
> code posted and decided to try it. It fails. I read the
> tutorial and the entry for the built in function sum,
> but still do not see the problem. The code was cut and
> paste. Please help. Thanks.
>
> _________________________BEGIN_CODE_________________________
> #!/usr/bin/python
>
> ps = [none, 2,3,5,7,11,13,17,19,23,29]
>
> def phi(x, a):
>     return x - sum(phi(x // ps[i+1], i) for i in range(a))
>
> def pi(n):
>     from math import sqrt
>     if n <= 1:
>         return 0
>     a = pi(int(sqrt(n)))
>     return phi(n, a) + a - 1
> __________________________END_CODE__________________________
>
> Here is the result of running it:
>
>   File "/Users/mdp/source/prime_counter_python", line 6
>     return x - sum(phi(x // ps[i+1], i) for i in range(a))
>                                           ^
> SyntaxError: invalid syntax
>
> Here are some lines from python -v:
>
> Python 2.3 (#1, Sep 13 2003, 00:49:11)
> [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
> Type "help", "copyright", "credits" or "license" for more information
> 
> --
> Michael Press




More information about the Python-list mailing list