[Tutor] WAIDW - copying list within a recursive function
by Ole Jensen other posts by this author
Sep 1 2003 2:27AM messages near this date
Re: [Tutor] WxArt2d
|
Re: [Tutor] WAIDW - copying list within a recursive function
This is a multi-part message in MIME format.
------=_NextPart_000_006D_01C37041.4B9DE510
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
WAIDW =3D=3D (W)hat (A)m (I) (D)oing (W)rong
(Question put short:
Why does this function return; None?
###
def shuffle(i=3D24, deck=3D[]):
a =3D [ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king]
deck.append(a[:])
print deck
if i > 0:
print "if i > 0"
shuffle(i-1, deck)
elif i =3D=3D 0:
print "elif i =3D=3D 0"
print deck
return deck
else: print "failsafe"
###
Read below for more detailed describtion of my wrong doings ;)
)
I'm trying to create a list representing a stack of cards containing six =
card decks, I do not really care much of the about colour so my stack =
would come to look like this:
###
stack =3D [[ace, 2, 3,....., king], [ace, 2, 3,....., =
king],........,[ace, 2, 3,...., king]
###
The above should symbolise a list (stack) with 24 lists of different =
colours (allthough the colours e.g. clubs or hearts, are not shown as =
such).
Now call me lazy but I would prefer not to type all those those =
identical lists manually (both easthatically and labour wise), so I saw =
this as a good time to try to play around with recursive functions =
(easily done in a forloop with two lines, but now I'm stubborn).=20
The best bit of code I have come up with untill now is this:
###
def shuffle(i=3D24, deck=3D[]):
a =3D [ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king]
deck.append(a[:])
print deck
if i > 0:
print "if i > 0"
shuffle(i-1, deck)
elif i =3D=3D 0:
print "elif i =3D=3D 0"
print deck
return deck
else: print "failsafe"
###
(The print statements are for testing)
Which of course doesn't work (Otherwise I wouldn't be writing this =
mail)!
The the output ends up like this:
[[11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]]
if i > 0
[[11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10], [11, 2, 3, 4, 5, 6, 7, 8, =
9, 10, 10, 10, 10]]
if i > 0
etc. etc.
These few lines above shows me that everything shaping about nicely, the =
list is looking the way it's supposed to.
In the end of the output i finally equals 0, and the list is printed a =
final time looking complete (len(deck) =3D=3D 24). When the next command =
in the sourcecode reads: return deck.
###
elif i =3D=3D 0:
print "elif i =3D=3D 0"
print deck
return deck
###
My question is this:
If the list "deck" is as it is supposed to (and it looks to be), then =
why does the function return; None?
In advance thanks for any enlightment.
------=_NextPart_000_006D_01C37041.4B9DE510
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML> <HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2730.1700" name=3DGENERATOR>
<STYLE> </STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV> <FONT face=3DArial size=3D2>WAIDW =3D=3D (W)hat (A)m (I) (D)oing=20
(W)rong</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2></FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>(Question put short:</FONT></DIV>
<DIV> Why does this function return; None?</DIV>
<DIV> <FONT face=3DArial size=3D2>
<DIV> <FONT face=3DArial size=3D2>###</FONT></DIV>
<DIV> <FONT face=3DArial size=3D2>def shuffle(i=3D24, =
deck=3D[]):</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2> a =3D [ace, 2, 3, 4, =
5, 6, 7, 8,=20
9, 10, jack, queen, king]<BR> =20
deck.append(a[:])<BR> print deck<BR> =
if i=20
> 0:</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2> =
print "if i=20
> 0"<BR> shuffle(i-1, =
deck)</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2> elif i =3D=3D =
0:</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2> =
print "elif i=20
=3D=3D 0"<BR> print =
deck</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2> =20
return deck<BR> else: print =
"failsafe"</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>###</FONT></DIV>
<DIV> </DIV>
<DIV> Read below for more detailed describtion of my wrong doings =
;)</DIV>
<DIV> )</DIV></FONT></DIV>
<DIV> <FONT face=3DArial size=3D2></FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>I'm trying to create a list =
representing=20
a stack of cards containing six card decks, I do not really care =
much of=20
the about colour so my stack would come to look like this:</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2></FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>###</FONT></DIV>
<DIV> <FONT face=3DArial size=3D2>stack =3D [[ace, 2, 3,....., king], =
[ace, 2, 3,.....,=20
king],........,[ace, 2, 3,...., king]</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>###</FONT></DIV>
<DIV> <FONT face=3DArial size=3D2></FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>The above should symbolise a list =
(stack) with 24=20
lists of different colours (allthough the colours e.g. clubs or hearts, =
are not=20
shown as such).</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2></FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>Now call me lazy but I would prefer not =
to type all=20
those those identical lists manually (both easthatically and labour =
wise),=20
so I saw this as a good time to try to play around with recursive=20
functions (easily done in a forloop with two lines, but now I'm =
stubborn).=20
</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>The best bit of code I have come up =
with untill now=20
is this:</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2></FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>###</FONT></DIV>
<DIV> <FONT face=3DArial size=3D2>def shuffle(i=3D24, =
deck=3D[]):</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2> a =3D [ace, 2, 3, 4, =
5, 6, 7, 8,=20
9, 10, jack, queen, king]<BR> =20
deck.append(a[:])<BR> print deck<BR> =
if i=20
> 0:</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2> =
print "if i=20
> 0"<BR> shuffle(i-1, =
deck)</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2> elif i =3D=3D =
0:</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2> =
print "elif i=20
=3D=3D 0"<BR> print =
deck</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2> =20
return deck<BR> else: print =
"failsafe"</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>###</FONT></DIV>
<DIV> <FONT face=3DArial size=3D2>(The print statements are for =
testing)</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2></FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>Which of course doesn't work =
(Otherwise I=20
wouldn't be writing this mail)!</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>The the output ends up like =
this:</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>[[11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, =
10,=20
10]]<BR> if i > 0<BR>[[11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10], =
[11, 2, 3,=20
4, 5, 6, 7, 8, 9, 10, 10, 10, 10]]</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>if i > 0</FONT></DIV>
<DIV> <FONT face=3DArial size=3D2>etc. etc.</FONT></DIV>
<DIV> <FONT face=3DArial size=3D2>These few lines above shows me that =
everything=20
shaping about nicely, the list is looking the way it's supposed =
to.</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2></FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>In the end of the output i finally =
equals 0, and=20
the list is printed a final time looking complete (len(deck) =3D=3D 24). =
When the=20
next command in the sourcecode reads: return deck.</FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2></FONT> </DIV>
<DIV> <FONT face=3DArial size=3D2>###</FONT></DIV>
<DIV> <FONT face=3DArial size=3D2>elif i =3D=3D 0:</DIV>
<DIV> <FONT face=3DArial size=3D2> print "elif i =3D=3D =
0"<BR> print deck</FONT></DIV>
<DIV> <FONT face=3DArial size=3D2> =20
return deck<BR> ###</FONT></DIV>
<DIV> </DIV>
<DIV> My question is this:</DIV>
<DIV> If the list "deck" is as it is supposed to (and it looks to be), =
then why=20
does the function return; None?</DIV>
<DIV> </DIV>
<DIV> In advance thanks for any enlightment.</DIV>
<DIV> </DIV></FONT></BODY></HTML>
------=_NextPart_000_006D_01C37041.4B9DE510--
Attachments:
unknown1
Thread:
Ole Jensen
Lloyd Kvam
|