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:
sellerWhen the same function is called using this line of code:
fun("p", "i", "p", "n")it should produce the following output:
pippin6.3 Writing Code
Write a complete program in Python that meets the following specification:
- It should include a function named
displaythat 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
squarethat will call thedisplayfunction multiple times to create an ASCII art square as shown in the output example below. Thesquareprocedure may not use theprint(expression)statement directly - it must call thedisplayfunction to produce output. - It should include a
mainfunction that only calls thesquarefunction. - It should call the
mainfunction at the end of the code to start the program.
When the program is run, it should produce the following output:
* * * *
* * * *
* * * *
* * * *