Skip to main content
Engineering LibreTexts

3.14: Exercises

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Exercise \(\PageIndex{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.

    >>> right_justify('monty')

    Use string concatenation and repetition. Also, Python provides a built-in function called len that returns the length of a string, so the value of len('monty') is 5.

    Exercise \(\PageIndex{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):

    Here’s an example that uses do_twice to call a function named print_spam twice.

    def 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 a parameter. There should be only two statements in the body of this function, not four.

    Exercise \(\PageIndex{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=' ')

      The output of these statements is '+ -' on the same line. The output from the next print statement would begin on the next line.

    2. Write a function that draws a similar grid with four rows and four columns.


    This exercise is based on an exercise in Oualline, Practical C Programming, Third Edition, O’Reilly Media, 1997.

    This page titled 3.14: Exercises is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?