Skip to main content

Section 16.16 Write Code Exercises

Activity 16.16.1.

Complete the following code to create an object spot from the Dog class with the name “Spot” and update their tricks to include “spin” then “sit”. You can use print(spot) to print the string representation of the object.
Solution.
class Dog:

    def __init__(self, name):
        self.name = name
        self.tricks = []    # creates a new empty list for each dog

    def updateTricks(self, trick):
        self.tricks.append(trick)

    def __str__(self):
        return f'Dog(name = {self.name}, tricks = {str(self.tricks)})'

spot = Dog('Spot')
spot.updateTricks('spin')
spot.updateTricks('sit')
print(spot.tricks)
print(spot)

====

from unittest.gui import TestCaseGui

class myTests(TestCaseGui):

    def testOne(self):
        self.assertEqual(spot.tricks, ['spin', 'sit'], 'Checking that sit and spin are added to the list of tricks.')
        self.assertEqual(spot.__str__(), "Dog(name = Spot, tricks = ['spin', 'sit'])", 'Checking that a Spot prints correctly.')
myTests().main()

Activity 16.16.2.

Complete the following code to include a method named updateAttacks(attack) which appends the attack to the list of attacks.

Activity 16.16.3.

Correct the 7 errors in the following code. The program should create a class that prints the title and author of a book.
Solution.
class Book:

    def __init__(self, title, author):
        self.title = title
        self.author = author

    def __str__(self):
        return f"Your book is {self.title} by {self.author}"

book = Book("The Odyssey", "Homer")
print(book)

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
    def testOne(self):
        gatsby = Book("The Great Gatsby", "F. Scott Fitzgerald")
        self.assertEqual(gatsby.__str__(), "Your book is The Great Gatsby by F. Scott Fitzgerald", "Testing Great Gatsby")

myTests().main()

Activity 16.16.4.

Correct all the errors in the following code. The program should create a class that prints the name the tricks the dog knows.

Activity 16.16.5.

Complete the following code to include a method named getTitle that returns the title and a method named getAuthor that returns the ‘author’.
Solution.
class Book:

    def __init__(self, title, author):
        self.title = title
        self.author = author

    # Create the methods using self to access the attributes
    def getTitle(self):
        return self.title

    def getAuthor(self):
        return self.author


book = Book("The Odyssey", "Homer")
print(book.getTitle())
print(book.getAuthor())

Activity 16.16.6.

Complete the following code to include a method named getTricks that returns the tricks list and a method named getName that returns the name when called.

Activity 16.16.7.

Add a new class named Paperback that extends the Book class. Add a method named __str__ within Paperback that sends a string representation for the Paperback book, reading "Paperback book [TITLE] was written by [AUTHOR]".
Solution.
class Book:

    def __init__(self, title, author):
        self.title = title
        self.author = author

    def getTitle(self):
        return self.title

    def getAuthor(self):
        return self.author

# Create Paperback class, using Book class
class Paperback(Book):

    def __str__(self):
        return f"Paperback book {self.title} was written by {self.author}"

book = Paperback("The Odyssey", "Homer")
print(book)

Activity 16.16.8.

Update the new class named WaterType which inherits properties of the Pokemon class. Add the following three methods to WaterType: updateAttacks appends the attacks list with a new attack, getName returns the name, and getAttacks returns the attacks when called.

Activity 16.16.9.

Add a new class named WaterType that inherits from Pokemon class. that takes ‘name’ as initial values, creates an instance of Pokemon with type as ‘water’ by default and stores the instance in a list named ‘watertypes’. Also create a method called ‘addPokemons’ which takes ‘name’ as arguments, creates an instance of Pokemon and stores it in ‘watertypes’. Also create ‘__str__’ that returns the string representation of the object that includes the ‘watertypes’ list.
You have attempted of activities on this page.