[Tutor] WAIDW - copying list within a recursive function
Ole Jensen
learning.python at dbmail.dk
Mon Sep 1 05:27:04 EDT 2003
WAIDW == (W)hat (A)m (I) (D)oing (W)rong
(Question put short:
Why does this function return; None?
###
def shuffle(i=24, deck=[]):
a = [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 == 0:
print "elif i == 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 = [[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).
The best bit of code I have come up with untill now is this:
###
def shuffle(i=24, deck=[]):
a = [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 == 0:
print "elif i == 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) == 24). When the next command in the sourcecode reads: return deck.
###
elif i == 0:
print "elif i == 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20030901/590eb7f9/attachment.htm
More information about the Tutor
mailing list