473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

syntax error in sum(). Please explicate.

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
8 3663
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
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
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

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
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
[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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: u473 | last post by:
I am scratching my head with the required quotes and parentheses. I started with an existing working Query with Running Total by date. Now I need to produce a running total by quarter day, so I...
1
by: vkalvakota | last post by:
Hi, I am getting the following error when i am trying to create a view. But if i run the select statement independently it is working fine. Please help me it is very urgent. ...
2
by: Rshah | last post by:
sql query syntax? i write this query but error get : column not found how can i write ? SELECT c.stock_cat_name, a.stock_code, c.description, a.date, IIF(MID(a.type,2,1)="I",SUM(a.quantity),0)...
1
by: jomurf | last post by:
Not sure if this is the right forum. I'm using Microsoft Query to get data from our ERP. However, every time I try to sum a column, I get a syntax error. Here's the sql statement: SELECT...
10
by: jonathanemil | last post by:
Hello, I am a 1st semester Computer Science student in a Python class. Our current assignment calls for us to read a list from a file, create a 2-dimensional list from the file, and check to see...
0
by: goal2007 | last post by:
Ok I am tying to convert access syntax to Sql syntax to put it in a stored procedure or view.. Here is the part that I need to convert: SELECT .proj_name, .task_name, .Employee, ...
2
by: Mangler | last post by:
I created a query in SQL that works fine but when i put it in the asp code ( with dreamweaver ) i am getting an error when viewing the page: expected end of statment... ...
3
by: Chrizo | last post by:
Hi, I am new to C++ and I cannot seem to figure out what is wrong with my code. It looks fine to me but when I try to create an object of class Location I get this error which I do not understand...
7
by: Yesurbius | last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.