Page 66 -
P. 66

recursive function




                                              You were to call the function that you’ll create print_lol(). It
                                              takes one argument: a list to display on screen. You were to grab your
                                              pencil and complete the code to provide the required functionality:

                            def print_lol(the_list):

              Process the provided   for each_item in the_list:         If the item being processed

              list with a “for” loop.                                   function.
                                                                        is itself a list, invoke the


                                              isinstance(each_item, list):
                                          if
                                                      print_lol(each_item)
                                          else:                              If the item being processed ISN’T
                                                      print(each_item)       a list, display the item on screen.








                Let’s use IDLE one final time to test your new function. Will it work as well as your earlier code?

               >>> def print_lol(the_list):
                      for each_item in the_list:
                          if isinstance(each_item, list):     Define the function.
                              print_lol(each_item)
                          else:
                              print(each_item)


               >>> print_lol(movies)
                                              Invoke the function.
               The Holy Grail
              1975
               Terry Jones & Terry Gilliam
              91
               Graham Chapman                      It works, too! The recusrive function
               Michael Palin                       produces EXACTLY the same results as
                                                  the earlier code.
              John Cleese
               Terry Gilliam
               Eric Idle
               Terry Jones



           30    Chapter 1
   61   62   63   64   65   66   67   68   69   70   71