Skip to main content
Engineering LibreTexts

4.E: Functions (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 4: What is the purpose of the "def" keyword in Python?

    a) It is slang that means "the following code is really cool"
    b) It indicates the start of a function
    c) It indicates that the following indented section of code is to be stored for later
    d) b and c are both true
    e) None of the above

    Exercise 5: What will the following Python program print out?

    def fred():
    def jane():

    a) Zap ABC jane fred jane
    b) Zap ABC Zap
    c) ABC Zap jane
    d) ABC Zap ABC
    e) Zap Zap Zap

    Exercise 6: Rewrite your pay computation with time-and-a-half for overtime and create a function called computepay which takes two parameters (hours and rate).

    Enter Hours: 45
    Enter Rate: 10
    Pay: 475.0

    Exercise 7: Rewrite the grade program from the previous chapter using a function called computegrade that takes a score as its parameter and returns a grade as a string.

    Score   Grade
    > 0.9     A
    > 0.8     B
    > 0.7     C
    > 0.6     D
    <= 0.6    F
    Program Execution:
    Enter score: 0.95
    Enter score: perfect
    Bad score
    Enter score: 10.0
    Bad score
    Enter score: 0.75
    Enter score: 0.5

    Run the program repeatedly to test the various different values for input.

    This page titled 4.E: Functions (Exercises) is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Chuck Severance 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?