In: Computer Science
# Mini-Exercise 4: Classes practice # Complete the three methods that say 'your code here' in the classes below class Character: ''' This class represents a character in a game. ''' def __init__(self, name, health=100): '''(str, int) -> Character Construct a character with the name and health provided. If health is not provided, a default health of 100 is used. ''' self.name = name self.health = health def __str__(self): '''() -> str Return a string representation of this character. ''' return "{}, health = {}".format(self.name, self.health) def is_alive(self): '''() -> bool Return True iff the character has health greater than 0. ''' # YOUR CODE HERE pass def punch(self, opponent): '''(Character) -> None Given an opponent Character, punch that opponent (decrease that opponent's health by 10). If decreasing by 10 would make the opponent's health less than 0, just set the health to 0 instead. (That is, the health should never be a negative number. ''' # YOUR CODE HERE pass class Game: def __init__(self, characters): '''(Game, list of Characters) -> None Construct a new Game with the given list of Characters. ''' self.characters = characters def add_character(self, c): '''(Game, Character) -> None Given a Character c, add c to this game's list of characters. ''' self.characters.append(c) def revive_characters(self): '''(Game) -> None Revive any character in the Game's list of characters whose health is 0 by increasing their health to 100. ''' # YOUR CODE HERE pass # Some lines to try out your code;feel free to add more if __name__ == "__main__": character1 = Character("Wizard", 500) character2 = Character("Monster") print(character1) print(character2) character1.punch(character2) print(character1.health) print(character2.health)
CODE
class Character:
'''
This class represents a character in a game.
'''
def __init__(self, name, health=100):
'''(str, int) -> Character
Construct a character with the name and health provided.
If health is not provided, a default health of 100 is used.
'''
self.name = name
self.health = health
def __str__(self):
'''() -> str
Return a string representation of this character.
'''
return "{}, health = {}".format(self.name, self.health)
def is_alive(self):
'''() -> bool
Return True iff the character has health greater than 0.
'''
return self.health > 0
def punch(self, opponent):
'''(Character) -> None
Given an opponent Character, punch that opponent (decrease
that opponent's health by 10). If decreasing by 10 would
make the opponent's health less than 0, just set the health
to 0 instead. (That is, the health should never be a negative
number.
'''
opponent.health -= 10;
if opponent.health < 0:
opponent.health = 0
class Game:
def __init__(self, characters):
'''(Game, list of Characters) -> None
Construct a new Game with the given list of Characters.
'''
self.characters = characters
def add_character(self, c):
'''(Game, Character) -> None
Given a Character c, add c to this game's list of characters.
'''
self.characters.append(c)
def revive_characters(self):
'''(Game) -> None
Revive any character in the Game's list of characters whose health
is 0 by increasing their health to 100.
'''
for character in self.characters:
if character.health == 0:
character.health += 100
# Some lines to try out your code;feel free to add more
if __name__ == "__main__":
character1 = Character("Wizard", 500)
character2 = Character("Monster")
print(character1)
print(character2)
character1.punch(character2)
print(character1.health)
print(character2.health)