I'm writing a game and I have two variables:
strength = 10
hp = 50 + (strength * 5)
I was able to build a system that lets me update the strength variable as well as other attributes, but I assumed hp would be updated when I updated strength, which isn't the case.
strength += skilladdnum
print("you now have", strength, "strength!")
Is there any way for hp to update when strength updates?
hpto always be equal to50 + strength * 5(or some other formula), you could just define a functionget_hp()that calculates and returns that value. Then just callget_hp()whenever you need the currenthpvalue. This way you can also change the formula without needing to change your code everywhere.hpthat uses this formula whenever it is used in a calculation or printed. But then you'd be reinventingsympy, so you might be better off usingsympyif you want this behavior. The best option long-term is probably to create a class to manage all the character info, with getter/setter functions, as others have suggested.sympyis useless here. I'd stick withget_hp()inside aplayerclass.sympymuch), butsympyis most similar to what @zephergoose was expecting, i.e., the ability to define one variable as being algebraically related to another one. But for a real application, I like @drew-e's solution best: a class that encompasses all hero attributes, and does whatever calculations are needed behind the scenes.