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


The code for a function called by another function must be placed before or after the code that calls it?    before^ *before *$
Below is a function that calls another function. Code the first line of the called function. Its parameter is named number. def call_another_function()
  calc(50)
def calc(number):^ *def calc\(number\): *$
The function that is being called needs to know the value of x. Rewrite the third line of this function so the function being called knows it. def do_something():
  x = 101
  calc():
calc(x) calc`(`x`)
What is the parameter name of the function called by this code? calc(number = -5)numbernumber
Code the first line of the function that is being called. Make up the parameter name. analyze("O'Leary") def analyze(name): ^ *def analyze\([a-zA-Z_][a-zA-Z0-9_]*\): *$
Replace the second line of this code with a function call. Make up the function name. Remember to indent. def x():
  y = "boogie" + "woogie"
  y = combine("boogie", "woogie") ^ ? ?y = [a-zA-Z_][a-zA-Z0-9_]*\(•boogie•, •woogie•\) *$
The function being called displays the result of dividing the first number by the second number. Code the function using just two lines of code. calc(x = 12, y = 3) def calc(x, y):
  print(x / y)
^ *def calc\(x, y\):\n print\(x / y\) *$
The function being called returns the result of dividing the first number by the second number. Code the function using just two lines of code. Make up the parameter names. result = calc(12, 3) def calc(x, y):
  return x / y
^ *def calc\(([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*)\):\n return \1 / \2 *$
Code a function that calls this function. Pass two keyword arguments that are integers. Make everything up. def calc(**numbers):
  for a_number in numbers.values():
    print(a_number)
def calls_another_function():
  calc(first_number = 12, second_number = 1400)
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(\):\n calc\([a-zA-Z_][a-zA-Z0-9_]* = -?[0-9]+, [a-zA-Z_][a-zA-Z0-9_]* = -?[0-9]+\) *$
Rewrite this code, replacing 2 + 4 with a function call that includes the two numbers as parameters and does the math. Make up the function name. def calc():
  return 2 + 4
def calc():
  return do_math(2, 4)
^ *def calc\(\):\n return [a-zA-Z_][a-zA-Z0-9_]*\(2, 4\) *$
  1. Code a function that displays something. The value to display is its only parameter.
  2. Code another function that calls the first function, passing it your name.
  3. Call the second function.
  4. Click the Run button   above your code. If you've coded correctly, your name will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
68233d43b5
  1. Code a function that calls this function, displaying the results.
  2. Call the function you coded, passing three arguments that are numbers.
  3. Click the Run button   above your code. If you've coded correctly, the result of the multiplication will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
d39d4b4278