Answered step by step
Verified Expert Solution
Question
1 Approved Answer
4. (20 pts) Consider the class Pokemon In [ ]: # Remember to run this cell # DO NOT DELETE OR MODIFY THIS CELL class
4. (20 pts) Consider the class Pokemon In [ ]: # Remember to run this cell # DO NOT DELETE OR MODIFY THIS CELL class Pokemon: def _init_(self, species, level, health): self.species = species self.level = level self.health = health Write a subclass My Pokemon . For full credit, your subclass should not contain species, level, or health attributes/fields of its own; it should rely on those in the superclass This class creates objects that can be iterated over using a for loop. The following should work: p = MyPokemon ("Pikachu", 10, 3) for field in p: print(field) should print the values of all three fields: Pikachu 10 3 MyPokemon has a method train(), which increases the level field by 1. The following should work: >>> p = MyPokemon ("Pikachu", 10, 3) >>> print("before train(), level =", p.level) before train(), level = 10 >>> p.train) >>> print("after train(), level =", p.level) after train(), level = 11 Overload the operator > by defining a __gt__ method so that we can compare the levels of two MyPokemon objects. For the following two MyPokemon objects A and B , B > A returns True because the level of B is greater than the level of A >>> A = MyPokemon ("Pikachu", 10, 3) >>> B = MyPokemon ("Charmander", 12, 3) >>> print(B > A) True >>> print(A > B) False . MyPokemon also has a method attack that simulates an attack from a MyPokemon object to another. The attack causes 1 point damage to the target's health field and prints a message about the attack. If the health field of the target is zero before the attack, raise a RuntimeError exception and print an error message "The target Pokemon has lost all health points". The following should work: >>> A = MyPokemon ("Pikachu", 10, 2) >>> B = MyPokemon("Charmander", 12, 1) >>> A.attack(B) Pikachu attacks Charmander doing 1 damage >>> print(B.health) 0 >>> try: A. attack (B) except RuntimeError as e: print(e) The target Pokemon has lost all health points In [ ]: # put your code here Test your class using the code below. It should print the values in all three fields twice. In [ ]: # test your code here # DO NOT DELETE p = My Pokemon ("Pikachu", 10, 3) for field in p: print(field) for field in p: print(field) Run the test cell below as well. The result should be before train(), level = 10 after train(), level = 11 Pikachu attacks Charmander doing 1 damage Pikachu attacks Charmander doing 1 damage Pikachu attacks Charmander doing 1 damage The target Pokemon has lost all health points In [ ]: # test your code here # DO NOT DELETE A = My Pokemon ("Pikachu", 10, 3) B = My Pokemon("Charmander", 10, 3) print("before train, level =", A.level) A.train() print("after train(), level =", A.level) if A > B: while True: try: A. attack(B) except RuntimeError as e: print(e) break In [ ]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started