MONT 105S, Spring 2009
Home | | Course Schedule | | Assignments | | Lecture NotesLaboratory 8
The Hero class
Creating the class.
Properties:
Methods (functions):
Part 1:
Use the following template to test your class so far:
Sample output: The output should look as follows (user input is underlined):
Part 2:
Change your main program so that it tests these new methods as follows:
Sample output: The output should look as follows (user input is underlined):
Part 3:
Change your main program to test these new methods as follows:
Sample output: The output should look as follows (user input is underlined):
What To Turn In. Reminder. Be sure to save a copy of
each program on your
Due at the end of today's class
In this lab you will create the Hero class. A hero is an object that has
a name, an inventory of items that he or she carries, and some number of health points.
This class would be useful if you were developing an adventure game in which a hero
journeys through a world collecting objects and fighting monsters.
Create the Hero class with the following properties and methods:
This method should set a property, called name, to the value of the parameter name.
self.name = name
This should return a string, "I am the hero, " plus the hero's name,
e.g. "I am the hero, Lancelot"
This should append item to the hero's inventory.
This should print "The following items are in the inventory."
It should then print each item (hint: use a for loop)
This should print that the hero has attacked a troll, e.g. "Lancelot has attacked a troll."
It should subtract 2 from the hero's health.
It should print the new health value, e.g. "Lancelot now has 8 health points."
This should search the inventory for a "potion".
If a potion is found, it should print "The healing potion has healed" the hero, and
it should set the hero's health to 10.
It should then print the hero's health, e.g. "Lancelot now has 10 health points."
Start by defining the Hero class with the two properties (inventory and health).
Add the __init__(self, name) method and the __str__(self) method.
#Program: heroClass.py
#Your program prologue goes here
class Hero:
#Add your properties and methods here (don't forget to indent)
#The main program starts here (to test the class)
heroName = raw_input("Please enter the hero's name: ")
myHero = Hero(heroName)
print myHero
Please enter the hero's name: Fred
I am the hero, Fred
Add the definitions of add_inventory(self, item) and print_inventory(self). Add these
methods after the __str__(self) method but before the main program.
heroName = raw_input("Please enter the hero's name: ")
myHero = Hero(heroName)
print myHero
myHero.add_inventory("sword")
myHero.add_inventory("potion")
myHero.add_inventory("armor")
myHero.print_inventory( )
Please enter the hero's name: Eowyn
I am the hero, Eowyn
The following items are in the inventory:
sword
potion
armor
Add the definitions of attack_troll(self) and heal(self). Add these after the
definition of print_inventory(self) but before the main program.
heroName = raw_input("Please enter the hero's name: ")
myHero = Hero(heroName)
print myHero
myHero.add_inventory("sword")
myHero.add_inventory("potion")
myHero.add_inventory("armor")
myHero.print_inventory( )
myHero.attack_troll( )
myHero.heal( )
Please enter the hero's name: Mario
I am the hero, Mario
The following items are in the inventory:
sword
potion
armor
Mario has attacked a troll.
Mario now has 8 health points.
The healing potion heals Mario
Mario now has 10 health points.