Exercise 14.3 Python provides a built-in function called len that returns the length of a string, sothe value of len(‘allen’) is 5.Write a function named right_justify that takes a string named s as a parameter and prints thestring with enough leading spaces so that the last letter of the string is in column 70 of the display.>>> right_justify(‘allen’)allenExercise 14.4 A function object is a value you can assign to a variable or pass as an argument. Forexample, do_twice is a function that takes a function object as an argument and calls it twice:def do_twice(f):f()f()Heres 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 thefunction twice, passing the value as an argument.3. Write a more general version of print_spam, called print_twice, that takes a string as aparameter and prints it twice.4. Use the modified version of do_twice to call print_twice twice, passing ‘spam’ as anargument.5. Define a new function called do_four that takes a function object and a value and calls thefunction four times, passing the value as a parameter. There should be only two statements inthe body of this function, not four.You can see my solution at thinkpython.com/code/do_four.py.14.10. Exercises 121Exercise 14.5 This exercise2 can be done using only the statements and other features we havelearned 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:print ‘+’, ‘-‘If the sequence ends with a comma, Python leaves the line unfinished, so the value printednext appears on the same line.print ‘+’,print ‘-‘The output of these statements is ‘+ -‘.A print statement all by itself ends the current line and goes to the next line.2. Use the previous function to draw a similar grid with four rows and four columns.You can see my solution at thinkpython.com/code/grid.py.
https://academichomeworks.com/wp-content/uploads/2021/06/logo-300x75.png 0 0 admin https://academichomeworks.com/wp-content/uploads/2021/06/logo-300x75.png admin2021-08-10 06:24:362021-08-10 06:24:36https://qualityhomeworkanswers.com/engineering-homework-help-54-2/
We are a professional writing service that provides original papers. Our products include academic papers of varying complexity and other personalized services, along with research materials for assistance purposes only.