1

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?

4
  • If you want hp to always be equal to 50 + strength * 5 (or some other formula), you could just define a function get_hp() that calculates and returns that value. Then just call get_hp() whenever you need the current hp value. This way you can also change the formula without needing to change your code everywhere. Commented Aug 9, 2021 at 23:06
  • You could also define a custom class for hp that uses this formula whenever it is used in a calculation or printed. But then you'd be reinventing sympy, so you might be better off using sympy if 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. Commented Aug 9, 2021 at 23:14
  • @MatthiasFripp sympy is useless here. I'd stick with get_hp() inside a player class. Commented Aug 10, 2021 at 4:33
  • @Suthiro Practically speaking that may be true (I haven't used sympy much), butsympy is 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. Commented Aug 10, 2021 at 4:36

3 Answers 3

2

On the level of language there is no way to somehow natively "tie together" twot variables so one would update automatically every time the other one gets updated if that's what you're thinking.

The proper way to do it would be to specify a "setter" function which you would call to update the strength and it would update also the hp. So you could have something like

def set_strength(new_strength):
    return new_strength, 50 + (new_strength * 5)

strength, hp = set_strength(strength + skilladdnum)

but that's a bit ugly and does not feel idiomatic. Once you get into classes and object oriented programming then you'll have e.g. class Character which will have fields (variables) hp and strength and from the outside of such class you would use only functions like set_strength and inside the class Character you would take care of updating them properly on all places.

Sign up to request clarification or add additional context in comments.

Comments

1

HP would go in a function, and you call it whenever you want its value:

strength = 50

def hp():
    return 50 + (strength * 5)

If this is used in a class, you can use the property decorator to get the value from the character: ie, character.strength, character.hp. It would look similar:

class Hero:

    def __init__(self, strength):
        self.strength = strength
    
    @property
    def hp(self):
        return 50 + (self.strength * 5)

Comments

0

If I understand you correctly, you would like hp to change whenever strength is updated, by the formula given as hp = 50 + (strength * 5)

However, when you assign hp in the line hp = 50 + (strength * 5) for the first time, you are initializing the variable hp to be the result of said formula at the given state. hp is not tied in any way to strength after the assignment.

Thus, you should update hp each time you update strength.

You can do this in a 'Pythonic' way with a function that updates both together to make it one line in the main code and cleaner. This can also be done in a more advanced fashion with a lambda or property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.