Question

Please code in Python: A function can be assigned to a variable and passed into another...

Please code in Python:

A function can be assigned to a variable and passed into another function as an argument. Consider the following function that executes a function twice:

def exec_2(f):
    f()
    f()

any function that is passed into exec_2() will be executed twice without parameters. Your task is to write a function exec_4(f) that takes in a function f as a parameter and executes the function f 4 times using the function exec_2. The function f in this case will draw a cute koala bear, and, in conjunction with, exec_4(), you will draw 4 koalas on the same tree! Suppress the newline in the print statement for the koala, this can be done using print's end argument.

Default template code:

koala = """
             |       :     . |
             | ' :      '   |
             | . |   ' | |
   .--._ _...:.._ _.--. , ' |
( , `        ` , )   . |
   '-/              \-' |   |
     | o   /\   o |       :|
     \     _\/_     / : '   |
     /'._   ^^   _.;___      |
   /`    `""""""`      `\=   |
/`                     /= .|
;             '--,-----'=    |
|                 `\ |    . |
\                   \___ :   |
/'.                     `\= |
\_/`--......_            /= |
            |`-.        /= : |
            | : `-.__ /` .   |
            |    .   ` |    '|
            | . : `   . | |"""


def exec_2(f):
    f()
    f()

def exec_4(f):
    # define this function using exec_2
  
# define a new function here that prints out the koala once

# use your function and exec_4 to draw 4 koala bears!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the python code to print 4 koala bears by calling the functions.

There are sufficient comments in the code for you to understand.

Note: You may look the screenshot of the code to have better understanding of the indentation.

Python code:


#function to print koala bear once
def f():
koala = """
| : . |
| ' : ' |
| . | ' | |
.--._ _...:.._ _.--. , ' |
( , ` ` , ) . |
'-/ \-' | |
| o /\ o | :|
\ _\/_ / : ' |
/'._ ^^ _.;___ |
/` `""""""` `\= |
/` /= .|
; '--,-----'= |
| `\ | . |
\ \___ : |
/'. `\= |
\_/`--......_ /= |
|`-. /= : |
| : `-.__ /` . |
| . ` | '|
| . : ` . | |"""
#print koala variable
print(koala, end='')
  
#function to print twice koala
def exec_2():
#call function f() twice
f()
f()
  
#function to print koala 4 times
def exec_4():
#call function exec_2() twice
exec_2()
exec_2()
  
#call function exec_4() to print bear 4 times
exec_4()

Output:

Add a comment
Know the answer?
Add Answer to:
Please code in Python: A function can be assigned to a variable and passed into another...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT