Question: 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 THISCELL class Pokemon: def _init_(self, species, level, health): self.species = species self.level

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!