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


Rewrite this function call so it includes 100 as an argument. add_numbers() add_numbers(100)^ *add_numbers\(100\) *$
Rewrite this first line of this function definition so it includes number as a parameter. def display_number(): def display_number(number):^ *def display_number\(number\): *$
Call the function add_numbers, passing 10 and 20 as arguments. add_numbers(10, 20) add_numbers`(`10`, `20`)
Code the first line of a function definition. The name of the function is greet. The parameter is greeting. def greet(greeting):def greet(greeting):
Call a function, passing a variable as the argument. Make up the names. greet(greeting) ^ *[a-zA-Z_][a-zA-Z0-9_]*\([a-zA-Z_][a-zA-Z0-9_]*\) *$
Code the first line of a function definition that has two parameters. Make everything up. def add_numbers(first_number, second_number): ^ *def [a-zA-Z_][a-zA-Z0-9_]*\([a-zA-Z_][a-zA-Z0-9_]*, [a-zA-Z_][a-zA-Z0-9_]*\): *$
In two lines, code a function that displays the sum of its two parameters. Make everything up. def sum_numbers(first_number, second_number):
  print(first_number + second_number)
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*)\):\n print\(\1 \+ \2\) *$
Call a function, passing a variable and a string as arguments. Make everything up. greet_personally(standard_greeting, "Humberto") ^ *[a-zA-Z_][a-zA-Z0-9_]*\([a-zA-Z_][a-zA-Z0-9_]*, •.+•\) *$
Code a function that has two parameters. If the values of the two arguments are the same, display "same" def compare(x, y):
  if x == y:
    print("same")
^ *def [a-zA-Z_][a-zA-Z0-9_]*\(([a-zA-Z_][a-zA-Z0-9_]*), ([a-zA-Z_][a-zA-Z0-9_]*)\):\n if \1 == \2:\n print\(•same•\) *$
Code a function that has three parameters. The first argument is the name of a dictionary. The second and third arguments are a key and a value. The function adds the key and value to the dictionary. Make everything up. def add_to_dict(dict, key, value):
  dict[key] = value
^ *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_]*)\):\n \1\[\2\] = \3 *$
  1. You have to define a function before you can call it. But it'll be easier if you leave room at the top for the function definition and code the function call first. Then you can go back to the top and code the function.
  2. On line 9 call the function, passing the variables sales_total and tax_rate as arguments.
  3. Code the function. It calculates the tax on a sales total and displays a message.
  4. The function's two parameters are the total and the tax rate.
  5. Calculate the tax by multiplying the two parameters.
  6. Convert the result of the multiplication to a string.
  7. Display the concatenation of "The tax is " and the tax string.
  8. Click the Run button   above your code. If you've coded correctly, The tax is 3.0 will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
1244081e35
  1. On line 9 ask the user to enter a color and assign her answer to a variable.
  2. On line 15 call a function, passing it the name of the tuple I've coded and the name of the variable holding the user's color.
  3. Go to line 2 and code the function.
  4. The function loops through the tuple to see if the value of the variable is in it. If so, it displays a message and breaks the loop.
  5. Click the Run button   above your code. If you've coded correctly, the input query will display in the right-hand panel. If you enter one of the primary colors, your message will display. (You can click Instructions at the top of the right-hand panel to see the correct code.)
139d5e5f21