A Smarter Way to Learn Python / Chapter 47 Exercises

  • Index of exercises
  • Email me

1

2

3

4

5

6

7

8

9

10

11

12

 

Congratulations. You've aced all the exercises for this chapter.


To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to Trinket.


Post a review on Amazon for the paperback or the Kindle edition.


Email me to give me a compliment, complaint, or correction. I'll respond.


  All of my books include free interactive online exercises that make the knowledge stick.

A Smarter Way to Learn JavaScript
Shop for it at Amazon
A Smarter Way to Learn HTML and CSS
Shop for it at Amazon
A Smarter Way to Learn jQuery
Shop for it at Amazon

copyright © 2017 by Mark Myers. All rights reserved.

0,1,2,3,4,5,6,7,8,9,10,11

0

0

0

0


To pass information back from a function to the code that called it, you use the keyword ______ return^ *return *$
The calling code needs to provide a _______ that will receive the value that is returned by the function. variable^ *variable *$
To pass the result of the math operation back to the calling code, code the last line of the function. def calc():
  total = 2 + 2
return total return `total
Call the function calc, which will return a value. The variable that will receive the returned value is named result. result = calc()result = calc()
Code a function, named trivial, that does nothing but pass the value 2 back to the calling code. def trivial():
  return 2
^ *def trivial\(\):\n return 2 *$
Call a function that receives a value returned by the function. The function call has no arguments. Make everything up. result = calc() ^ *[a-zA-Z_][a-zA-Z0-9_]* = [a-zA-Z_][a-zA-Z0-9_]*\(\) *$
Code a function that adds 2 plus 2, assigns the result to a variable, and passes the result back to the calling code. Make up the function name and variable. def calc():
  tot = 2 + 2
  return tot
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(\):\n ([a-zA-Z_][a-zA-Z0-9_]*) = 2 \+ 2\n return \1 *$
This time, code a function that adds 2 + 2, but condense the code into two lines. Don't assign 2 + 2 to a variable. Pass the result itself back to the calling code. Make up the name of the function. def calc():
  return 2 + 2
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(\):\n return 2 \+ 2 *$
Call a function, but this time, instead of assigning the returned value to a variable, just display the returned value. Make up the name of the function. Code the function and the print statement in a single line. print(calc()) ^ *print\([a-zA-Z_][a-zA-Z0-9_]*\(\)\) *$
Code a three-line function that creates a list with three integers. Pass the list back to the calling code. Make everything up. def make_list():
  numbers = [12, 24, 36]
  return numbers
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(\):\n ([a-zA-Z_][a-zA-Z0-9_]*) = \[-?[0-9]+, -?[0-9]+, -?[0-9]+\]\n return \1 *$
  1. Code a function that performs a calculation on its two parameters and passes the result back
  2. Call the function, passing two positional arguments that are integers.
  3. Display the result produced by the function call.
  4. There are two opportunities here to condense your code. Take them if you wish.
  5. Click the Run button   above your code. If you've coded correctly, the result of the calculation will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
78c52d539f
  1. I've code a dictionary. Code a function that will change one of the values in the dictionary.
  2. The function has three parameters—the name of a dictionary, a dictionary key, and a dictionary value. The function uses these parameters to change one of the values in the dictionary. Then it returns the revised dictionary.
  3. Call the function, providing three keyword arguments—the name of the dictionary, the key for the animal whose sound you're going to change, and the new sound for the animal.
  4. Display the revised dictionary.
  5. Click the Run button   above your code. If you've coded correctly, the revised dictionary will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
e7ef534476