A function object is a value you can assign to a variable or pass as an argument. For example, do_twice is a function that takes a function object as an argument and calls it twice: def do_twice
Book: Think Python
These are my solutions for exercises in chapter “functions”.
If you have suggestions, do not hesitate to give them down below 😀
[sourcecode lang="python"] #THINK PYTHON - BOOK NAME #CHAPTER - "FUNCTIONS" #COMPLETED! ''' Exercise 3.1 Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display. ''' def right_justify(s): length_of_str = len(s) #sets the length of the given string leading_spaces = 70 - length_of_str return (" " * leading_spaces) + s ''' Exercise 3.2. A function object is a value you can assign to a variable or pass as an argument. For example, do_twice is a function that takes a function object as an argument and calls it twice: def do_twice(f): f() f() Here’s an example that uses do_twice to call a function named print_spam twice. def print_spam(): print('spam') do_twice(print_spam) 1. Type this example into a script and test it. 2. Modify do_twice so that it takes two arguments, a function object and a value, and calls the function twice, passing the value as an argument. 3. Copy the definition of print_twice from earlier in this chapter to your script. 4. Use the modified version of do_twice to call print_twice twice, passing 'spam' as an argument. 5. Define a new function called do_four that takes a function object and a value and calls the function four times, passing the value as ''' def print_spam(value): print(value) def do_twice(f, value): f(value) f(value) def do_four(f, value,repeat=4): do_repeats(f, value, repeat) def do_repeats(f, value, repeat): for x in range(repeat): f(value) ''' Exercise 3.3. Note: This exercise should be done using only the statements and other features we have learned so far. 1. Write a function that draws a grid like the following: + - - - - + - - - - + | | | | | | | | | | | | + - - - - + - - - - + | | | | | | | | | | | | + - - - - + - - - - + Hint: to print more than one value on a line, you can print a comma-separated sequence of values: print('+', '-') By default, print advances to the next line, but you can override that behavior and put a space at the end, like this: print('+', end=' ') print('-') The output of these statements is '+ -' on the same line. The output from the next print statement would begin on the next line. ''' ''' Write a function that draws a similar grid with four rows and four columns. ''' def print_grid(num_spaces = 4, height = 8, width=2): ''' Empty columns - ! in row num_spaces * constant ''' number_of_headers = int(round(height / num_spaces)) # round to int for row in range(height + number_of_headers): has_plus = row % (num_spaces + 1) #only when non divisible by num_spaces if has_plus == 0: print(("+" + "-" * num_spaces) * width + "+" ) else: print(( "|" + " " * num_spaces )* width + "|") #heehe cheating but okay. print(("+" + "-" * num_spaces) * width + "+" ) [/sourcecode]