Give a repeated task a name — quick review
A recipe card titled Tea sits in a kitchen drawer.
A def line and its indented body define a function without running the body. The functions of this lesson have no parameters. Once such a function has been defined, a call, written as its name followed by an empty pair of parentheses, runs the body from its first line. When the body finishes, the program carries on from the place of the call. Each such call runs the body once.
Find the lines that the program needs in more than one place.
Write
def, a name that says what the lines do, an empty pair of parentheses and a colon.Put the lines under the
defline and indent them one more level, four spaces, keeping any deeper indentation they already have.Leave the indented block, and write the function's name with parentheses at each place where the lines should run.
Trace the program: skip the body at the
defline, and run the body at each call.
The parentheses make the call. On a line below the function's definition, the name alone, show_banner with no parentheses, does not call it. The file displays no banner and reports no error. Check for the parentheses when a function seems to do nothing.
The body belongs to the function only while it is indented. A print line at the margin, after the body, is not part of the function. It runs once, when Python reaches it, and not at each call.