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


This is the first line of a method. Fill in the blank.   def calc(______): self^ *self *$
This is the first line of a method. Fill in the blank.   def say_if_minor________ (self):^ *\(self\): *$
Code the first line of a method. Its name is whatever. def whatever(self): def `whatever`(`self`)`:
Code the first line of a method. Its name is x. Start by indenting.  def x(self): def x(self):
Code the first line of a method. Make up the name of the method. Indent.   def calc(self): ^ * def [a-zA-Z_][a-zA-Z0-9_]*\(self\): *$
Code the second line of a method. Display the name attribute of the instance. Indent two tabs.     print(self.name) ^ * print\(self\.name\) *$
Code the first two lines of a method. It displays an attribute of the instance. Make up the name of the method and the name of the attribute. Indent the first line. Indent the second line two tabs.   def show(self):
    print(self.price)
^ * def [a-zA-Z_][a-zA-Z0-9_]*\(self\):\n print\(self\.[a-zA-Z_][a-zA-Z0-9_]*\) *$
Code a method. If the age attribute of the instance equals the height attribute of the instance, display a message. Make up what you need to. Indent correctly, starting with the first line.   def compare(self):
    if self.age == self.height:
      print("Old adult!")
^ * def [a-zA-Z_][a-zA-Z0-9_]*\(self\):\n if self\.age == self\.height:\n print\(•.+•\) *$
Code a method. Concatenate two attributes and assign the combination to a variable. Make everything up. Begin by indenting.   def combine(self):
    combination = self.first + self.last
^ * def [a-zA-Z_][a-zA-Z0-9_]*\(self\):\n [a-zA-Z_][a-zA-Z0-9_]* = self\.[a-zA-Z_][a-zA-Z0-9_]* \+ self\.[a-zA-Z_][a-zA-Z0-9_]* *$
Code the first four lines of a class definition. It has one attribute. The fourth line—and the last one that you're going to code—is the first line of a method. Make everything up. Indent correctly. class Customer():
  def __init__(self, age):
    self.age = age
  def check_age(self):
^ *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\): *$
  1. Code a class with one attribute and one method. The method displays the attribute.
  2. Code an instance of the class.
  3. Call the method.
  4. Click the Run button   above your code. If you've coded correctly, the value of the attribute will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
dcfd71c5b7
  1. Code a class with two attributes and one method. The attributes are integers. The method compares the first value with the second value. If the first value is greater than the second value, display a message.
  2. Code an instance of the class. Make the value of the first attribute greater than the value of the second attribute.
  3. Call the method.
  4. Click the Run button   above your code. If you've coded correctly, the message will display in the right-hand panel. (You can click Instructions at the top of the right-hand panel to see the correct code.)
e30eb0b8e4