1. Define and call
A function is a reusable block of code with a name. You define it with def, a name, parentheses and a colon. The body is indented four spaces:
defstarts the definition;function_nameis the name you choose.- The empty parentheses mean the function takes no parameters.
- The body runs only when you call the function: its name followed by parentheses.
- Defining a function prints nothing; calling it can be repeated as many times as you need.
Define and call a function
Define a function named say_hello with no parameters. Its body must print Hello!. Then call it once.
Print exactly:
Create a free account to save your progress and unlock chapter 2.
Call the function twice
Define say_hello with no parameters. Its body must print Hello!. Call it twice, one call after the other. Each call runs the body again.
Print exactly:
Create a free account to save your progress and unlock chapter 2.
Put several statements in the body
A function body can contain more than one statement. Define show_menu with no parameters. Its body must print Coffee, then Tea, in that order. Call it once.
Print exactly:
Create a free account to save your progress and unlock chapter 2.
Call a function inside a loop
Define cheer with no parameters. Its body must print Go!. Use a for loop over range(3) and call cheer() inside the loop.
Print exactly:
Create a free account to save your progress and unlock chapter 2.