A Smarter Way to Learn Python / Chapter 66 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


A Python program that stores functions that you can access from other Python programs is called a _____________ module^ *module *$
To use functions stored in a module, you must _______ the module. import^ *import *$
The name of a module must end in _____. .py .`py
Rewrite this import statement so it's legal. import calc.pyimport calcimport calc
Rewrite this import statement so it's legal. import "functions.py" import functions ^ *import functions *$
Code an import statement. Make up the name of the module. import math ^ *import [a-zA-Z_][a-zA-Z0-9_]* *$
Call the function add_tax in the calculations module. calculations.add_tax() ^ *calculations\.add_tax\(\) *$
Call a function in a module. Make everything up. calculations.add_tax() ^ *[a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]*\(\) *$
Import a module. Then call a function in the module. Make everything up. import calculations
calculations.add_tax()
^ *import ([a-zA-Z_][a-zA-Z0-9_]*)\n\1\.[a-zA-Z_][a-zA-Z0-9_]*\(\) *$
  1. Code a function to be included in the calculations module. It accepts two integers as parameters. In a single line, it adds the two integers together and returns the result.
  2. Call the function, passing two integers as arguments. (The function call would actually be in your main Python program, not grouped with the function in the module, but don't worry about that.) Remember that you need a variable for storing the returned value. Make up the name of the function and the name of the variable. When calling the function, assume that the module has already been imported.
def add_em(first_number, second_number):
  return first_number + second_number
total = calculations.add_em(133, 102)
^ *def ([a-zA-Z_][a-zA-Z0-9_]*)\(([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*)\):\n return \2 \+ \3\n[a-zA-Z_][a-zA-Z0-9_]* = [a-zA-Z_][a-zA-Z0-9_]*\.\1\(-?[0-9]+, -?[0-9]+\) *$
  1. In IDLE (See Appendix D), code a Python program that greets the user. Save it to the Desktop.
  2. Code a second Python program that imports the first program and calls the function. Save it to the Desktop.
  3. Run the second program.
  4. If you've coded correctly, your greeting will display.