syntax error in sum(). Please explicate.

M

Michael Press

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
 
T

tom

Michael said:
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
 
M

Matt Moriarity

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

Paul Rubin

Michael Press said:
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)

Generator comprehensions like you used weren't introduced til
Python 2.4, so 2.3 will raise a syntax error like you got.
 
J

John Machin

Michael said:
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.

I doubt it -- "none" should be "None"

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

New syntax. Python 2.3 is *two* versions behind the current production
version. Either (a) run it on Python 2.4 or 2.5 or (b) enclose the
argument to sum in square brackets i.e.

sum([blah blah blah]) instead of sum(blah blah blah)

Cheers,
John
 
T

Tim Peters

[Matt Moriarity]
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))

[Michael Press]
Thank you. That makes it work.

But is a wrong solution ;-) As others have suggested, it's almost
certainly the case that you were using a too-old version of Python.
2.5 is current, and 2.4.4 (the current bugfix release in the 2.4 line)
would also work. You must have been using a version before 2.4, and
if you're just starting with Python, it's a Bad Idea to artificially
burden yourself with an old version where current examples can't work
;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,301
Latest member
BuyPureganics

Latest Threads

Top