Page 88 -
P. 88
add an argument
Control behavior with an extra argument
If you add an extra argment to your function, you can handle indentation
within your current code without too much trouble.
Yikes! I should’ve thought about that
myself... I probably need to go easy on
the coffee. Of course, it’s clear to me
now: adding another argument to your
function gives you options.
Take your function to the next level
At the moment, your function has a single argument: the_list. If you add
a second argument called level, you can use it to control indentation. A
positive value for level indicates the number of tab-stops to include when
displaying a line of data on screen. If level is 0, no indentation is used; if
it’s 1, use a single tab-stop; if it’s 2, use two tab-stops; and so on.
It’s clear you are looking at some sort of looping mechanism here, right? You
already know how to iterate over a variably sized list, but how do you iterate a
fixed number of times in Python?
Does Python provide any functionality that can help?
52 Chapter 2