ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-Tutor
python-Tutor
Re: [Tutor] WAIDW - copying list within a recursive function
by Lloyd Kvam other posts by this author
Sep 1 2003 3:21AM messages near this date
[Tutor] WAIDW - copying list within a recursive function | [Tutor] problems with win32com.client and excel.
This function will return a None if it ends though your failsafe else OR
if it ends through a non-zero i.  It is not ending through the failsafe
branch since we do not see the printing of failsafe.

When the i==0 path returns the deck, it is returning it to the i==1
invocation of shuffle. That invocation does NOT have an explicit return,
so it returns None to the i == 2 invocation and so on.

The fix is:
....
     if i >  0:
         print "if i >  0"
         shuffle(i-1, deck)
         return deck
....

Ole Jensen wrote:

>  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.
>   
>  
>  
>  ------------------------------------------------------------------------
>  
>  _______________________________________________
>  Tutor maillist  -  Tutor@[...].org
>  http://mail.python.org/mailman/listinfo/tutor

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582


_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Ole Jensen
Lloyd Kvam

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved