Page 95 -
P. 95

sharing your code


          Work out what’s wrong


           There’s your problem: the recursive invocation of your function is using the
           old function signature that required only one argument. The new version of
           your function requires two arguments.
           The fix is easy: provide the correct number of arguments when calling the
           new version of your function. So, this snippet of code from your function:


                                    if isinstance(each_item, list):
                                         print_lol(each_item)


           needs to be rewritten to specify the correct number of arguments:



                                    if isinstance(each_item, list):
                                         print_lol(each_item, level)





                                         Not so fast. Surely the nested
                                   list needs to be printed after a specific
                                   number of tab-stops? At the moment, your
                                   code sets “level” to 0 but never changes the
                                   value, so “level” never has any impact on your
                                        displayed output...




                                          Right. Your use of “level” needs one final tweak.
                                          The whole point of having level as an argument is to allow you to
                                          control the nested output. Each time you process a nested list, you need
                                          to increase the value of level by 1. Your code snippet needs to look
                                          like this:



                                                if isinstance(each_item, list):
                                                    print_lol(each_item, level+1)


                                                                       Simply increment the value of level by 1 each

           It’s time to perform that update.                           time you recursively invoke your function.

                                                                                       you are here 4    59
   90   91   92   93   94   95   96   97   98   99   100