another puzzled newbie

E

Elaine Jackson

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+=(-1)

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

Steven Taschuk

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

Peter Otten

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
 
C

Chad Netzer

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+=(-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].
 
E

Erik Max Francis

Elaine said:
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:
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.
 
A

Andrew MacIntyre

| > for i in range(len(shuffler)):
| > shuffler+=(-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 += -1

YMMV...

Regards,
Andrew.
 
C

Chad Netzer

| > for i in range(len(shuffler)):
| > shuffler+=(-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 += -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.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top