Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language= python Consider the class Pokemon In (): Remember to run this cell DO NOT DELETE OR MODIFY THIS CELL class Pokemon: def _init__(self, species,

Language= python

image text in transcribedimage text in transcribed

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 MyPokemon. 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 MyPokenon 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 traino), 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", 18, 3) >>> B = MyPokemon ("Charmander", 12, 3) >>> print(B > A) True >>> print (A > B) False MyPokenon also has a method attack() that simulates an attack from a My Pokemon 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 danage >>> print(B.health) >>> try: A.attack(0) 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 (): 4 test your code here # DO NOT DELETE P = MyPokemon("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 (): 4 test your code here 4 DO NOT DELETE A = MyPokemon ("Pikachu", 10, 3) B = MyPokemon("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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Next Generation Databases NoSQLand Big Data

Authors: Guy Harrison

1st Edition

1484213300, 978-1484213308

More Books

Students also viewed these Databases questions

Question

7. It is advisable to do favors for people whenever possible.

Answered: 1 week ago

Question

9. Power and politics can be destructive forces in organizations.

Answered: 1 week ago