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


In the following code, what is the value of total if the function call doesn't pass any arguments? def add_numbers(first_number = 2, second_number = 4):
  total = first_number + second_number
6^ *6 *$
In the following code, what is the value of total? def add_numbers(first_number = 2, second_number = 4):
  total = first_number + second_number
add_numbers(second_number = 10)
12^ *12 *$
Code the first line of a function, analyze, that has a single parameter, name, with a default of "Jones". def analyze(name = "Jones"): def `analyze`(`name `= `"`Jones`"`)`:
Code the first line of a function, x, that has two parameters, y and z. The first parameter has no default. The second parameter has a default of 1. def x(y, z = 1):def x(y, z = 1):
Rewrite this code so it's legal. def x(y = 1, z): def x(z, y = 1):
The problem could be the parameter order, or could be something else. Parameters with defaults must come after parameters
without defaults. Also, check your spacing.
^ *def x\(z, y = 1\): *$
Code the first line of a function with one parameter whose default is a string. Make everything up. def test(name = "Park"): ^ *def [a-zA-Z_][a-zA-Z0-9_]*\([a-zA-Z_][a-zA-Z0-9_]* = •.+•\): *$
Call this function. Don't override the defaults. def x(y = "Bulgaria", z = 1.89): x() ^ *x\(\) *$
Code the first line of a function. It has one parameter without a default and a second parameter whose default is an integer. Make everything up. def product_invoice(product_name, price = 1): ^ *def [a-zA-Z_][a-zA-Z0-9_]*\([a-zA-Z_][a-zA-Z0-9_]*, [a-zA-Z_][a-zA-Z0-9_]* = -?[0-9]+\): *$
Code the first line of a function. It has three parameters. All of them have defaults. The first parameter is the name of a list. The second parameter is a string. The third parameter is an integer. Make everything up. def find_info(lst = customer_list, status =
"premier", limit = 100):
^ *def [a-zA-Z_][a-zA-Z0-9_]*\([a-zA-Z_][a-zA-Z0-9_]* = [a-zA-Z_][a-zA-Z0-9_]*, [a-zA-Z_][a-zA-Z0-9_]* = •.+•, [a-zA-Z_][a-zA-Z0-9_]* = -?[0-9]+\): *$
Code a call to this function that makes full_name equal "Hugo Jones". Don't pass any information to the function that you don't have to pass. def assemble_name(last_name = "Jones", first_name = "John"):
  full_name = first_name + " " + last_name
assemble_name(first_name = "Hugo") ^ *assemble_name\(first_name = •Hugo•\) *$
  1. Code a function that has two parameters, both of them numbers. Assign a default value to each parameter. The function does an arithmetic operation using the two numbers and displays the result.
  2. Call the function without passing arguments.
  3. Click the Run button   above your code. If you've coded correctly, the result of the arithmetic operation will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
766eff77e9
  1. Run this code. See that it displays hammock: 12
  2. Revise the function call to make rug: 144 display.
  3. (You can click Instructions at the top of the right-hand panel to see the correct code.)
01cf4db075