Question: Please write code in Python, greet classes and use inheritance. The code for Monster: class Monster: #init def __init__(self,name,type=UnknownType): if type !=UnknownType and type !=Yetti:

Please write code in Python, greet classes and use inheritance.

The code for Monster:

class Monster: #init def __init__(self,name,type="UnknownType"): if type !="UnknownType" and type !="Yetti": raise ValueError else: self.monsterType = type self.name=name self.weaponsDict = {} #get info def GetInfo(self): print(f"Name of the Monster {self.name} with type {self.monsterType}")

#Fight def Fight(self): counter = 0 for key in self.weaponsDict: counter = counter + self.weaponsDict[key] return counter

#Add weapon

def AddWeapon(self,weaponName,weaponPower): self.weaponsDict[weaponName] = weaponPower

#main function to test the class if __name__ == "__main__": #create a monster object - unknown type m1 = Monster("Monster1") #another object for type - Yetti m2 = Monster("Monster2","Yetti") #raise value error if a wrong type is supplied #Following code if uncommentd will return VALUEERROR #m3 = Monster("Monster3","xyz")

print("TEST: get info function on m2") m2.GetInfo()

print("TEST: add weapon to m2") m2.AddWeapon("Sword",5) m2.AddWeapon("Knife",4)

print("TEST: fight method ") print(m2.Fight())

Please extend the class Monster and define class Yeti

Yeti:

  • monsterType-string value = "Yeti"

  • name - string value, describes name of Yeti

  • weaponsDict - dictionary, contains information about weapons and their power. For example Sword=5,Knife=1. Monster has no weapons at the beginning

  • isHappy-True/False value, attribute defines if the monster is happy

methods

  • ChangeMood(isHappy)-function sets the value of is Happy attribute

  • Fight() - function returns 0 when Yetti is happy, otherwise returns 10

Please create 1 Monster and 1 Yetii. Please add weapons to the monsters. Please add monsters to the list and present the use of methods :Fight() and GetInfo()

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!