Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   another puzzled newbie (http://www.velocityreviews.com/forums/t321744-another-puzzled-newbie.html)

Elaine Jackson 08-28-2003 05:09 AM

another puzzled newbie
 
The analogous thing works fine in interactive mode, but when I put it in a
script, the function singlePass (below) causes a compile-time error.
(Interpret-time error? What I mean is, the message appears as soon as the shell
window opens.) Can anybody tell me what I'm stumbling over? As a point of
historical interest, this stuff was suggested by some material in Chapter 15 of
the ibiblio tutorial at http://www.ibiblio.org/obp/thinkCSpy/chap15.htm

Peace,
EJ

def shuffleList(preshuffled):
from random import randrange
n=len(preshuffled)
shuffler=[]
for i in range(n):
shuffler+=[randrange(i,n)]
return shuffler

def singlePass(shuffler,preshuffled,postshuffled):
postshuffled+=[preshuffled[shuffler[0]]
del(preshuffled[shuffler[0]])
del(shuffler[0])
for i in range(len(shuffler)):
shuffler[i]+=(-1)

def shuffle(preshuffled):
shuffler=shuffleList(preshuffled)
postshuffled=[]
for i in range(len(preshuffled)):
singlePass(shuffler,preshuffled,postshuffled)
preshuffled=postshuffled



Steven Taschuk 08-28-2003 05:52 AM

Re: another puzzled newbie
 
Quoth Elaine Jackson:
> The analogous thing works fine in interactive mode, [...]


I'm not sure what this means.

> [...] but when I put it in a
> script, the function singlePass (below) causes a compile-time error.

[...]
> postshuffled+=[preshuffled[shuffler[0]]


There's a ']' missing here, which explains the SyntaxError, in
interactive mode or otherwise.

(Btw, there's no need for the parentheses in a del statement;
del preshuffled[shuffler[0]] # etc.
is just as effective, and imho better style.)

--
Steven Taschuk staschuk@telusplanet.net
"[T]rue greatness is when your name is like ampere, watt, and fourier
-- when it's spelled with a lower case letter." -- R.W. Hamming


Peter Otten 08-28-2003 06:08 AM

Re: another puzzled newbie
 
Elaine,

it is always a good idea to post the exact error message (cut and paste),
(even if it is not particulary helpful in this case) and a short abstract
of what your posted piece of code is supposed to do. What I could find out
so far:

You have forgotten the last ] in the first line of singlePass():

postshuffled += [preshuffled[shuffler[0]]]

By the way, open (, [ or { lead Python to assume that the following lines
are also part of the list, tuple, dictionary or function call, which often
causes misleading error messages.

Hope that helps,
Peter




Chad Netzer 08-28-2003 06:09 AM

Re: another puzzled newbie
 
On Wed, 2003-08-27 at 22:09, Elaine Jackson wrote:
> The analogous thing works fine in interactive mode, but when I put it in a
> script, the function singlePass (below) causes a compile-time error.


A SyntaxError perhaps?

> def singlePass(shuffler,preshuffled,postshuffled):
> postshuffled+=[preshuffled[shuffler[0]]

^
You forgot a ']' at the end of the line

> del(preshuffled[shuffler[0]])

^
You don't need the '(' and ')' here. del is a keyword, not a function.

> del(shuffler[0])
> for i in range(len(shuffler)):
> shuffler[i]+=(-1)

^^^^
If you want a tuple here, it should be (-1,). It needs the comma.
But, since shuffler is a list, you probably really want [-1].


--
Chad Netzer <cnetzer@sonic.net>



Erik Max Francis 08-29-2003 02:05 AM

Re: another puzzled newbie
 
Elaine Jackson wrote:

> "Chad Netzer" <cnetzer@sonic.net> wrote in message
> news:mailman.1062051053.30902.python-list@python.org...
>
> > You don't need the '(' and ')' here. del is a keyword, not a
> > function.

>
> Can't it be both? Could you explain why it fails to be a function?


Because it doesn't only not return a value, it _can't_ return a value:

>>> def f(x): print x

....
>>> f("This is a function")

This is a function
>>> returnValue = f("This is a function")

This is a function
>>> returnValue is None

True
>>> g = f
>>> del f
>>> returnValue = del f

File "<stdin>", line 1
returnValue = del f
^
SyntaxError: invalid syntax

Treating del as if it were a function is illegal. The same goes for the
other statements, like print.

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ The average dog is a nicer person than the average person.
\__/ Andrew A. Rooney

Andrew MacIntyre 08-29-2003 09:44 AM

Re: another puzzled newbie
 
On Fri, 29 Aug 2003, Elaine Jackson wrote:

> | > for i in range(len(shuffler)):
> | > shuffler[i]+=(-1)
> | ^^^^
> | If you want a tuple here, it should be (-1,). It needs the comma.
> | But, since shuffler is a list, you probably really want [-1].
>
> No, I want (-1) the integer. My problem is that I really dislike the appearance
> of a statement like X-=Y. (Even right now I can barely stand writing it.) So I
> wrote X+=(-Y) instead. Maybe it would look better as shuffler=map(lambda n: n-1,
> shuffler)?


I find a little white space makes things more attractive and easier to
read, ie:

shuffler[i] += -1

YMMV...

Regards,
Andrew.

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: andymac@bullseye.apana.org.au (pref) | Snail: PO Box 370
andymac@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia


Chad Netzer 08-29-2003 05:21 PM

Re: another puzzled newbie
 
On Fri, 2003-08-29 at 02:44, Andrew MacIntyre wrote:
> On Fri, 29 Aug 2003, Elaine Jackson wrote:
>
> > | > for i in range(len(shuffler)):
> > | > shuffler[i]+=(-1)
> > | ^^^^
> > | If you want a tuple here, it should be (-1,).


> > No, I want (-1) the integer.


> I find a little white space makes things more attractive and easier to
> read, ie:
>
> shuffler[i] += -1


Right. In my case, the first version led me to believe that the poster
may have wanted to append a tuple (well, construct a new one with
'+'...). The second one, with spaces, makes the intent much less
ambiguous at first glance.

--
Chad Netzer




All times are GMT. The time now is 10:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.