syntax error in sum(). Please explicate.

Michael Press jack at abc.net
Sat Nov 18 17:23:42 EST 2006


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