I’m writing a mini game to learn Python. I created a weapons class that can be imported into my main.py file.
Here is the class I made:
class weapon(object):
def __init__(self, name):
self.weaponName = name
def weaponStrength(self, level, strength):
self.weaponLevel = level
self.weaponStrength = strength
damage = self.weaponStrength * level
print "Damage is equal to %r" % damage
return damage
Here are the objects that are created using the weapons class.
# Creates an Object called sword using the weaponsClass
sword = weapon("sword")
# Calls a method of the weaponsClass to calculate weapon Strength. Returns a int
sword.weaponStrength(3, 20)
# Creates an Object called Magic Staff using the weaponsClass
magicStaff = weapon("Magic Staff")
# Calls a method of the weaponsClass to calculate weapon Strength. Returns a int
magicStaff.weaponStrength(5, 30)
# Sets a variable
swordStrength = sword.weaponStrength
# Sets a variable
magicStaffStrength = magicStaff.weaponStrength
# Prints the variable
print swordStrength
# Prints the variable
print magicStaffStrength
I’m trying to figure out why the swordStrength and magicStaffStrength are equal to the strength value passed to the method.
Any help is much appreciated.
Thanks.
you’re overwriting weaponStrength in the
weaponnamespace:and
are actually conflicting. Maybe think about your naming conventions