Page 100 -
P. 100
idle session
Amend your code to give the level argument a default value of 0 and then load your code into the IDLE editor.
Press F5 to load the code into the shell and then follow along to confirm that the latest version of your function
works as expected. Start be defining a short list of lists and use the function to display the the list on screen:
>>> names = ['John', 'Eric', ['Cleese', 'Idle'], 'Michael', ['Palin']]
>>> print_lol(names, 0)
John
Eric
The standard behavior works
Cleese as expected, with nested
Idle lists indented.
Michael
Palin
Now try to do the same thing without specifiying the second argument. Let’s rely on the default value kicking in:
>>> print_lol(names)
John
Eric
Cleese Without specifying the second argument, the
Idle default is used and works, too.
Michael
Palin
Now specify a value for the second argument and note the change in the function’s behavior:
>>> print_lol(names, 2)
John
Eric
Specify an alternative value for the second
Cleese argument and the indenting starts from
Idle that level.
Michael
Palin
One final example provides what looks like a silly value for the second argument. Look what happens:
>>> print_lol(names, -9)
John
count for “level” is unlikely to become a positive integer. This looks exactly
Eric Using a negative value effectively switches OFF the indenting, as the
Cleese
Idle like the original output from version 1.0.0, right?
Michael
Palin
64 Chapter 2