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  
Share this Question
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. 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
 
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
 
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.  
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  
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  
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 tooold 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
;)  
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  
P: n/a

Bjoern Schliessmann wrote:
Are generator comprehensions and generator expressions the same?
the documentation uses the word "expression", not comprehension.
</F>   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 2770
 replies: 8
 date asked: Nov 18 '06
