Page 94 -
P. 94
line check
Trace your code
When you’re trying to work out what went wrong with a program that looks
like it should be OK, a useful technique is to trace what happens as each line
of code executes. Here’s the code that you are currently working with. At
only three lines long (remember: the creation of the list is one line of code), it
doesn’t look like it should cause any trouble:
import nester Thes two lines look OK.
movies = [ "The Holy Grail", 1975, "Terry Jones & Terry Gilliam",
91,["Graham Chapman", ["Michael Palin",
"John Cleese", "Terry Gilliam", "Eric Idle",
"Terry Jones"]]]
You are invoking the function with two arguments,
nester.print_lol(movies, 0) so that’s OK, too.
With the data assigned to the function’s arguments, the function’s code starts The “movies” list is assigned to
to execute on each data item contained within the passed-in list: “the_list”, and the value 0 is
assigned to “level”.
T o save space, the
entire comment is
not shown. def print_lol(the_list, level):
"""This function ... """
Process each item in
the list…
for each_item in the_list:
…then decide what if isinstance(each_item, list): If the data item is a
to do next based on list, recursively invoke
whether or not the print_lol(each_item) the function…hang on
data item is a list. else: a second, that doesn’t
look right!?
for tab_stop in range(level):
print("\t", end='')
print(each_item)
58 Chapter 2