I have a class in python for a figure with attributes name, health, strength, stealth, agility, weapons and money. There is a shop in the game I'm making to increase the value of any of the integer properties with a specific item. Each integer property can be increased by one of two different items with a different cost and buff strength. The issue I'm having is actually incrementing the attribute by the amount and saving the object.
Here's the code for the object:
class Figure:
def __init__(self, stats):
#create figure object
self.name = stats[0]
self.health = int(stats[1])
self.strength = int(stats[2])
self.stealth = int(stats[3])
self.agility = int(stats[4])
self.weapons = int(stats[5])
self.money = int(stats[6])
def show_person(self):
#show object attributes
print("\n\n{}\n\nHealth: {}\nStrength: {}\nStealth: {}\nCunning: {}\nWeapons: {}\nMoney: £{}".format(self.name.title(),self.health,self.strength,self.stealth,self.cunning,self.weapons, self.money))
def set_attr(self,attr,buff):
#increase character attribute by a variable amount
eval("self.{} = self.{} + {}".format(attr,attr,buff))
I might put friend.set_attr("stealth",10)
to increase friend's value of stealth by 10 where friend is a variable that contains one of these Figure objects but this error is thrown:
File Computer Science\python\oop game.py", line 21, in set_attr
exec(eval("self.{} = self.{} + {}".format(attr,attr,buff)))
File "<string>", line 1
self.agility = self.agility + 4
^
SyntaxError: invalid syntax
And I can't work out why.