By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

syntax error in sum(). Please explicate.

P: n/a
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
Nov 18 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
tom
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

Nov 18 '06 #2

P: n/a
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...@abc.netwrote:
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
Nov 18 '06 #3

P: n/a
Michael Press <ja**@abc.netwrites:
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.
Nov 18 '06 #4

P: n/a

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.
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

Nov 18 '06 #5

P: n/a
In article
<11**********************@h54g2000cwb.googlegroups .com>
,
"Matt Moriarity" <ma************@gmail.comwrote:
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))
Thank you. That makes it work.

--
Michael Press
Nov 18 '06 #6

P: n/a
[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
;-)
Nov 18 '06 #7

P: n/a
Paul Rubin wrote:
Generator comprehensions
Are generator comprehensions and generator expressions the same?

Regards,
Björn

--
BOFH excuse #35:

working as designed

Nov 19 '06 #8

P: n/a
Bjoern Schliessmann wrote:
Are generator comprehensions and generator expressions the same?
the documentation uses the word "expression", not comprehension.

</F>

Nov 19 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.