Functions Practice
Let’s try some simple practice problems. These problems are not graded - they are just for you to practice before doing the real exercises in the lab itself. You can find the answers below each question by clicking the button below each question.
6.1 Reading Code
Write the output that is displayed to the user after running the following Python program:
def foo(word):
print(word, end=" ")
def bar(word1, word2, word3):
foo(word1)
foo(word2)
print("don't make a", end=" ")
print(word3)
def main():
bar("two", "wrongs", "right")
main()
6.2 Writing Code
Construct a function (not an entire program) that meets the following specification. When the function is called using this line of code:
fun("s", "e", "l" "r")
it should produce the following output:
seller
When the same function is called using this line of code:
fun("p", "i", "p", "n")
it should produce the following output:
pippin
6.3 Writing Code
Write a complete program in Python that meets the following specification:
- It should include a function named
display
that requires a single parameter. When run, that function should print the value of the parameter to the terminal, but without a newline character at the end. - It should include a function named
square
that will call thedisplay
function multiple times to create an ASCII art square as shown in the output example below. Thesquare
procedure may not use theprint(expression)
statement directly - it must call thedisplay
function to produce output. - It should include a
main
function that only calls thesquare
function. - It should call the
main
function at the end of the code to start the program.
When the program is run, it should produce the following output:
* * * *
* * * *
* * * *
* * * *