syntax error in sum(). Please explicate.
tom
tom at t0mb.net
Sat Nov 18 23:32:08 CET 2006
Michael Press 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]
>
>
None has a capital N ^
> 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
>
>
can you paste the complete code? I'm not sure why that's a syntax error
there. with a bit of fudging it seems to run in the interpreter ok...
> 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
>
>
More information about the Python-list
mailing list