Reputation:
Trying to get the hang of classes, can anyone tell me how to get my method "attack" to add the baseDamage to the damage? currently it just prints the "damage" and doesn't add the baseDamage to it.
from random import randint
class Weapon:
def __init__(self, name, baseDamage, price):
self.name = name
self.baseDamage = baseDamage
self.price = price
def attack(self):
damage = randint(1,10)
self.baseDamage + damage
return damage
def main():
steelSword = Weapon("Steel Longsword", 10, 250)
print("Name:", steelSword.name, "\nDamage:", steelSword.baseDamage, "\nPrice:", steelSword.price,"coins")
print(steelSword.attack())
main()
Upvotes: 0
Views: 44
Reputation: 26590
You need to assign your new value back to self.baseDamage. Your line here:
self.baseDamage + damage
Is not storing the result anywhere. To do this, you want to assign it back to self.baseDamage
like this:
self.baseDamage += damage
To expand that, it is similar to writing it out like this:
self.baseDamage = self.baseDamage + damage
You could even further simplify and do this:
self.baseDamage += randint(1,10)
Also, you should make sure what it is you want to return. If you return damage, you are not returning the "baseDamage + damage". Maybe you want to return self.baseDamage after you performed your attack calculation?
Upvotes: 1
Reputation: 5518
self.baseDamage += damage
OR
self.baseDamage = self.baseDamage + damage
Upvotes: 0