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


Change the last_name attribute of cust_20 to "Elb" cust_20.last_name = "Elb"^ *cust_20\.last_name = •Elb• *$
Change the population attribute of hexton to 123. hexton.population = 123^ *hexton\.population = 123 *$
Change the last_name attribute of no444 to the value represented by new_last. no444.last_name = new_last no444`.`last_name `= `new_last
For the instance city33, call the method update, passing the argument 100. city33.update(100)city33.update(100)
Assign a new value, a string, to an attribute of an instance. Make everything up. area51.slogan = "Don't ask" ^ *[a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]* = •.+• *$
Add together two of an instance's attributes and assign the result to a third attribute of the instance. Make everything up. building15.total_area = building15.heated_area + building15.unheated_area ^ *([a-zA-Z_][a-zA-Z0-9_]*)\.[a-zA-Z_][a-zA-Z0-9_]* = \1\.[a-zA-Z_][a-zA-Z0-9_]* \+ \1\.[a-zA-Z_][a-zA-Z0-9_]* *$
Change the value of an attribute to a Boolean. Your choice of Boolean. Make up the name of the instance and the attribute name. agent007.outmanned = True ^ *[a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]* = True|False *$
Change the value of an attribute to the result returned by a function. The function has no arguments. Make everything up. screen7.now_playing = select_film() ^ *[a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]* = [a-zA-Z_][a-zA-Z0-9_]*\(\) *$
Change the value of an attribute to the result returned by a method. Both the attribute and the method are in the same class. The method has no arguments. Make everything up. factory20.output = factory20.double_output() ^ *([a-zA-Z_][a-zA-Z0-9_]*)\.([a-zA-Z_][a-zA-Z0-9_]*) = \1\.[a-zA-Z_][a-zA-Z0-9_]*\(\) *$
Code a class that has one attribute and one method. The method has one parameter. The method changes the attribute to the sum of itself plus the parameter. Use shorthand for this (+=). Make everything up. class Building():
  def __init__(self, area):
    self.area = area
  def add(self, additional_area):
    self.area += additional_area
^ *class [A-Z][a-zA-Z0-9_]*\(\):\n def __init__\(self, ([a-zA-Z_][a-zA-Z0-9_]*)\):\n self\.\1 = \1\n def [a-zA-Z_][a-zA-Z0-9_]*\(self, ([a-zA-Z_][a-zA-Z0-9_]*)\):\n self\.\1 \+= \2 *$
  1. Code a single line that projects barstow's population based on its growth rate.
  2. Click the Run button   above your code. If you've coded correctly, 52500.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.)
752b8cb9b0
  1. Change barstow's growth percentage to .07
  2. Project Barstow's population based on the revised growth rate.
  3. Click the Run button   above your code. If you've coded correctly, 53500.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.)
8c22bb577c