# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment
responses = initialize_assignment("12_pokemon_polymorphism_q", "week_7", "readings", assignment_points = 18.0, assignment_tag = 'week7-readings')
# Initialize Otter
import otter
grader = otter.Notebook("12_pokemon_polymorphism_q.ipynb")
๐ฅ Polymorphism in Python: Pokรฉmon Battle System#
In the Pokรฉmon universe, different Pokรฉmon species share common actions like attacking, using abilities, and evolving, but they execute these actions in different ways depending on their type.
This is similar to polymorphism in Python, where multiple classes can share the same method names but implement them differently. For example, both Charizard and Pikachu can use the attack() method, but Charizard might use Flamethrower, while Pikachu uses Thunderbolt.
Letโs explore how polymorphism allows us to create flexible and reusable Pokรฉmon battle systems in Python!
# Run this block of code by pressing Shift + Enter to display the question
from questions._12_pokemon_polymorphism_q import Question1
Question1().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._12_pokemon_polymorphism_q import Question2
Question2().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._12_pokemon_polymorphism_q import Question3
Question3().show()
Question 1 (Points: 11.0): โก Free Response: Implement Polymorphism in a Pokรฉmon Battle System#
Implement a Pokรฉmon battle system using polymorphism:
Create a base class
Pokemon
with:A method
attack()
that returns"A Pokรฉmon attacks!"
Create subclasses
Pikachu
andCharizard
that:Override
attack()
with"Pikachu uses Thunderbolt!"
and"Charizard uses Flamethrower!"
Create a function
battle(pokemon1, pokemon2)
that:Calls
.attack()
on both Pokรฉmon and prints the results.make sure that each attack is on a differnt line. This can be done using two print statements.
Example Usage#
p1 = Pikachu()
p2 = Charizard()
battle(p1, p2)
Expected Output:
Pikachu uses Thunderbolt!
Charizard uses Flamethrower!
...
# Example usage
p1 = Pikachu()
p2 = Charizard()
battle(p1, p2)
grader.check("Pokemon-Polymorphism-Classes")
Submitting Assignment#
Please run the following block of code using shift + enter
to submit your assignment, you should see your score.
from pykubegrader.submit.submit_assignment import submit_assignment
submit_assignment("week7-readings", "12_pokemon_polymorphism_q")