16.15. Write Code Exercises¶
Complete the following code to create an object
spotfrom theDogclass with the name “Spot” and update their tricks to include “spin” then “sit”. You can useprint(spot)to print the string representation of the object.Below the class definition, create an instance of the class Dog, assigning it to the variable
spot. Be sure to include a name when constructing the instance! This works similar to a function. To update the dog’s tricks, use the instance and dot notation to call the method, with the name of the trick as the argument. This needs to be done for each trick. Then, using the print function, you can print the dog’s tricks and the contents of the__str__method.-
Complete the following code to include a method named
updateAttacks(attack)which appends theattackto the list of attacks. Correct the 7 errors in the following code. The program should create a class that prints the title and author of a book.
classis a reserved word and must be lowercase.class methods must include the self parameter as a reference to the current instance of the class.
def __init__(self, title, author):Use dot notation with the self instance to access the title variable in the Book class.
self.title = titleUse dot notation with the self instance to access the author variable in the Book class.
self.author = authorUse dot notation with the self instance to access the title variable when using it in a string.
self.titleUse dot notation with the self instance to access the author variable when using it in a string.
self.authorUse the correct class name, in this case it is
Booknotnew Book.
-
Correct all the errors in the following code. The program should create a class that prints the name the tricks the dog knows.
Complete the following code to include a method named
getTitlethat returns the title and a method namedgetAuthorthat returns the ‘author’.Create the
getTitleandgetAuthormethods just as you would define a function. Include theselfparameter and use the self instance with dot notation to access title and author, as needed.-
Complete the following code to include a method named
getTricksthat returns the tricks list and a method namedgetNamethat returns the name when called. Add a new class named
Paperbackthat extends theBookclass. Add a method named__str__within Paperback that sends a string representation for the Paperback book, reading"Paperback book [TITLE] was written by [AUTHOR]".Create the
Paperbackclass just as you did theBookclass, but use theBookclass as the parameter. Define the__str__function as usual, using instances from the parent class.-
Update the new class named
WaterTypewhich inherits properties of thePokemonclass. Add the following three methods to WaterType:updateAttacksappends the attacks list with a new attack,getNamereturns the name, andgetAttacksreturns the attacks when called. -
Add a new class named
WaterTypethat 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.