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
  • Basic Python code needed Define the get_lines_from_file) function which is passed a filename as a parameter....

    Basic Python code needed Define the get_lines_from_file) function which is passed a filename as a parameter. The function reads the information from the file (corresponding to the filename parameter) and returns a list of strings where each element of the returned list corresponds to one line of the file. The list of strings which is returned by the function should not contain any newline characters. For example, if the file contains the text: 10 440 240 4 42 4 42...

  • # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python...

    # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are...

  • 1. Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys...

    1. Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are printed in alphabetical order. Note that...

  • can someone please help me write a python code for this urgently, asap Question: Write a Python function, minmp,...

    can someone please help me write a python code for this urgently, asap Question: Write a Python function, minmp, that is passed a file name and a metal alloy's formula unit structure*". The file will contain metal elements and their properties. The function will return a tuple of the element from the formula with the lowest melting point and that melting point Write a second function, molform, that will be called by the first function, to take the metal alloy's...

  • In C please Write a function so that the main() code below can be replaced by...

    In C please Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original maino: int main(void) { double milesPerHour, double minutes Traveled; double hours Traveled; double miles Traveled; scanf("%f", &milesPerHour); scanf("%lf", &minutes Traveled); hours Traveled = minutes Traveled / 60.0; miles Traveled = hours Traveled * milesPerHour; printf("Miles: %1f\n", miles Traveled); return 0; 1 #include <stdio.h> 3/* Your solution goes here */ 1 test passed 4 All tests passed...

  • c++ please include comments 4. Rewrite the code below including the definitions of the function prototypes...

    c++ please include comments 4. Rewrite the code below including the definitions of the function prototypes #include <iostream> #include <iomanip> #include <string> #include <stream> using namespace stod bool disjunction(bool.bool) bool negation(bool): bool implication(bool,bool bool equi bool,bool char btoc(bool value) return (value)?(T") ('F); string truthTable); int main( cout << truth Ta return 0; The functions conjunction), disjunction), negation), implication), and equivalence) should return the truth value of the conjunction, disjunction, negation, implication and equivalence connectives respectively. The functions truthTable) return a...

  • Python 2.7.14 Programming Assignment Shape Drawing With Notepad++(PLEASE READ AND FOLLOW THESE INSTRUCTIONS THOROUGLY AS THIS...

    Python 2.7.14 Programming Assignment Shape Drawing With Notepad++(PLEASE READ AND FOLLOW THESE INSTRUCTIONS THOROUGLY AS THIS ASSIGNMENT IS FOR PYTHON 2.7.14. ONLY AND I REALLY NEED THIS TO WORK!!! ALSO PLEASE HAVE THE CODE PROPERLY INDENTED, WITH WHATEVER VARIABLES DEFINED, WHATEVER IT TAKES TO WORK FOR PYTHON 2.7.14. I feel like nothing I do is working and I'm trying everything I can think of. ): In this assignment, the student will create a Python script that implements a series of...

  • PLEASE HELP! python code Problem 4. Define the function pythagorian_coprimes (n=100) that prints all pairs of...

    PLEASE HELP! python code Problem 4. Define the function pythagorian_coprimes (n=100) that prints all pairs of positive integer numbers (a, b) such that c = a + b is also a whole number and 1 <c<n. Include only those triples that are co-prime (do not have any common divisors other than 1). For example, (3, 4, 5) is okay but (30, 40, 50) should be skipped. Help: As a starting example, examine the function pythagorian_triples that yields all triples. Modify...

  • IT PYTHON QUESTION1 Consider the following Python code, where infile.txt and outfile.txt both exist in the current directory 'z' ) 。1d = open ( ' infile. txt ' , for line in o...

    IT PYTHON QUESTION1 Consider the following Python code, where infile.txt and outfile.txt both exist in the current directory 'z' ) 。1d = open ( ' infile. txt ' , for line in old: new.write (line) new.write') ne«.close () old.close) Which of the following options best describes the purpose or outcome of this code? O A copy of the file infile.txt is made (except in double line spacing) and saved as outfile.txt in the current directory. O A copy of the...

  • Please write a code in python! Define and test a function named posterize. This function expects...

    Please write a code in python! Define and test a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function, but it uses the given RGB values instead of black. images.py import tkinter import os, os.path tk = tkinter _root = None class ImageView(tk.Canvas): def __init__(self, image, title = "New Image", autoflush=False): master = tk.Toplevel(_root) master.protocol("WM_DELETE_WINDOW", self.close) tk.Canvas.__init__(self, master, width = image.getWidth(), height = image.getHeight())...

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