Variable X not updating when variables that should effect X change












10















I am a beginner at python. As most beginners, I am trying to make a text based rpg. I have finally gotten to the leveling up system and am at another roadblock. When the character levels up, they get to allot x skill points to a skill (add x to a variable x times). two of these variables effect the health of the character, however, when these two variables are changed by the user, the health variable stays the same. I have simplified my code for ease of reading (as my prose above is not that clear to begin with lol):



class Char:

def __init__(self, x, y):
self.str = x
self.con = y
self.hp = (self.con + self.str) / 2


player = Char(20, 20)


def main(dude):
print("strength: " + str(dude.str))
print("constitution: " + str(dude.con))
print("hp: " + str(dude.hp))
print("------")
action = input("press 1 to change str, 2 to change con")
if action == "1":
dude.str = dude.str + 10
main(dude)
elif action == "2":
dude.con = dude.con + 10
main(dude)
else:
main(dude)


main(player)


although these variables can increase by increments of ten in this situation, the hp remains at 20



how can I remedy this problem?



Thanks!










share|improve this question


















  • 1





    Nowhere in this code do you attempt to modify dude.hp....Do you want the hp to automatically update when you modify con or str?

    – Johnny Mopp
    Dec 19 '18 at 0:39













  • the relationships between variables aren't dynamic. If you say x=5 y=5 z=x+y, z is 10. it doesn't change if you set x=6 after the fact

    – SuperStew
    Dec 19 '18 at 0:39








  • 1





    @JohnnyMopp Yes I want it to auto-update, sorry if that was not clear

    – Dominic DiTaranto
    Dec 19 '18 at 0:40











  • @SuperStew How could I make them dynamic? again, I am pretty new to this. thanks for your help

    – Dominic DiTaranto
    Dec 19 '18 at 0:41











  • @DominicDiTaranto you can't really make them dynamic, but you could write some kind of update function in the Char class. You may be able to have it "listen" for variable changes, but i don't think that will be straight forward

    – SuperStew
    Dec 19 '18 at 0:42
















10















I am a beginner at python. As most beginners, I am trying to make a text based rpg. I have finally gotten to the leveling up system and am at another roadblock. When the character levels up, they get to allot x skill points to a skill (add x to a variable x times). two of these variables effect the health of the character, however, when these two variables are changed by the user, the health variable stays the same. I have simplified my code for ease of reading (as my prose above is not that clear to begin with lol):



class Char:

def __init__(self, x, y):
self.str = x
self.con = y
self.hp = (self.con + self.str) / 2


player = Char(20, 20)


def main(dude):
print("strength: " + str(dude.str))
print("constitution: " + str(dude.con))
print("hp: " + str(dude.hp))
print("------")
action = input("press 1 to change str, 2 to change con")
if action == "1":
dude.str = dude.str + 10
main(dude)
elif action == "2":
dude.con = dude.con + 10
main(dude)
else:
main(dude)


main(player)


although these variables can increase by increments of ten in this situation, the hp remains at 20



how can I remedy this problem?



Thanks!










share|improve this question


















  • 1





    Nowhere in this code do you attempt to modify dude.hp....Do you want the hp to automatically update when you modify con or str?

    – Johnny Mopp
    Dec 19 '18 at 0:39













  • the relationships between variables aren't dynamic. If you say x=5 y=5 z=x+y, z is 10. it doesn't change if you set x=6 after the fact

    – SuperStew
    Dec 19 '18 at 0:39








  • 1





    @JohnnyMopp Yes I want it to auto-update, sorry if that was not clear

    – Dominic DiTaranto
    Dec 19 '18 at 0:40











  • @SuperStew How could I make them dynamic? again, I am pretty new to this. thanks for your help

    – Dominic DiTaranto
    Dec 19 '18 at 0:41











  • @DominicDiTaranto you can't really make them dynamic, but you could write some kind of update function in the Char class. You may be able to have it "listen" for variable changes, but i don't think that will be straight forward

    – SuperStew
    Dec 19 '18 at 0:42














10












10








10








I am a beginner at python. As most beginners, I am trying to make a text based rpg. I have finally gotten to the leveling up system and am at another roadblock. When the character levels up, they get to allot x skill points to a skill (add x to a variable x times). two of these variables effect the health of the character, however, when these two variables are changed by the user, the health variable stays the same. I have simplified my code for ease of reading (as my prose above is not that clear to begin with lol):



class Char:

def __init__(self, x, y):
self.str = x
self.con = y
self.hp = (self.con + self.str) / 2


player = Char(20, 20)


def main(dude):
print("strength: " + str(dude.str))
print("constitution: " + str(dude.con))
print("hp: " + str(dude.hp))
print("------")
action = input("press 1 to change str, 2 to change con")
if action == "1":
dude.str = dude.str + 10
main(dude)
elif action == "2":
dude.con = dude.con + 10
main(dude)
else:
main(dude)


main(player)


although these variables can increase by increments of ten in this situation, the hp remains at 20



how can I remedy this problem?



Thanks!










share|improve this question














I am a beginner at python. As most beginners, I am trying to make a text based rpg. I have finally gotten to the leveling up system and am at another roadblock. When the character levels up, they get to allot x skill points to a skill (add x to a variable x times). two of these variables effect the health of the character, however, when these two variables are changed by the user, the health variable stays the same. I have simplified my code for ease of reading (as my prose above is not that clear to begin with lol):



class Char:

def __init__(self, x, y):
self.str = x
self.con = y
self.hp = (self.con + self.str) / 2


player = Char(20, 20)


def main(dude):
print("strength: " + str(dude.str))
print("constitution: " + str(dude.con))
print("hp: " + str(dude.hp))
print("------")
action = input("press 1 to change str, 2 to change con")
if action == "1":
dude.str = dude.str + 10
main(dude)
elif action == "2":
dude.con = dude.con + 10
main(dude)
else:
main(dude)


main(player)


although these variables can increase by increments of ten in this situation, the hp remains at 20



how can I remedy this problem?



Thanks!







python python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 19 '18 at 0:35









Dominic DiTarantoDominic DiTaranto

684




684








  • 1





    Nowhere in this code do you attempt to modify dude.hp....Do you want the hp to automatically update when you modify con or str?

    – Johnny Mopp
    Dec 19 '18 at 0:39













  • the relationships between variables aren't dynamic. If you say x=5 y=5 z=x+y, z is 10. it doesn't change if you set x=6 after the fact

    – SuperStew
    Dec 19 '18 at 0:39








  • 1





    @JohnnyMopp Yes I want it to auto-update, sorry if that was not clear

    – Dominic DiTaranto
    Dec 19 '18 at 0:40











  • @SuperStew How could I make them dynamic? again, I am pretty new to this. thanks for your help

    – Dominic DiTaranto
    Dec 19 '18 at 0:41











  • @DominicDiTaranto you can't really make them dynamic, but you could write some kind of update function in the Char class. You may be able to have it "listen" for variable changes, but i don't think that will be straight forward

    – SuperStew
    Dec 19 '18 at 0:42














  • 1





    Nowhere in this code do you attempt to modify dude.hp....Do you want the hp to automatically update when you modify con or str?

    – Johnny Mopp
    Dec 19 '18 at 0:39













  • the relationships between variables aren't dynamic. If you say x=5 y=5 z=x+y, z is 10. it doesn't change if you set x=6 after the fact

    – SuperStew
    Dec 19 '18 at 0:39








  • 1





    @JohnnyMopp Yes I want it to auto-update, sorry if that was not clear

    – Dominic DiTaranto
    Dec 19 '18 at 0:40











  • @SuperStew How could I make them dynamic? again, I am pretty new to this. thanks for your help

    – Dominic DiTaranto
    Dec 19 '18 at 0:41











  • @DominicDiTaranto you can't really make them dynamic, but you could write some kind of update function in the Char class. You may be able to have it "listen" for variable changes, but i don't think that will be straight forward

    – SuperStew
    Dec 19 '18 at 0:42








1




1





Nowhere in this code do you attempt to modify dude.hp....Do you want the hp to automatically update when you modify con or str?

– Johnny Mopp
Dec 19 '18 at 0:39







Nowhere in this code do you attempt to modify dude.hp....Do you want the hp to automatically update when you modify con or str?

– Johnny Mopp
Dec 19 '18 at 0:39















the relationships between variables aren't dynamic. If you say x=5 y=5 z=x+y, z is 10. it doesn't change if you set x=6 after the fact

– SuperStew
Dec 19 '18 at 0:39







the relationships between variables aren't dynamic. If you say x=5 y=5 z=x+y, z is 10. it doesn't change if you set x=6 after the fact

– SuperStew
Dec 19 '18 at 0:39






1




1





@JohnnyMopp Yes I want it to auto-update, sorry if that was not clear

– Dominic DiTaranto
Dec 19 '18 at 0:40





@JohnnyMopp Yes I want it to auto-update, sorry if that was not clear

– Dominic DiTaranto
Dec 19 '18 at 0:40













@SuperStew How could I make them dynamic? again, I am pretty new to this. thanks for your help

– Dominic DiTaranto
Dec 19 '18 at 0:41





@SuperStew How could I make them dynamic? again, I am pretty new to this. thanks for your help

– Dominic DiTaranto
Dec 19 '18 at 0:41













@DominicDiTaranto you can't really make them dynamic, but you could write some kind of update function in the Char class. You may be able to have it "listen" for variable changes, but i don't think that will be straight forward

– SuperStew
Dec 19 '18 at 0:42





@DominicDiTaranto you can't really make them dynamic, but you could write some kind of update function in the Char class. You may be able to have it "listen" for variable changes, but i don't think that will be straight forward

– SuperStew
Dec 19 '18 at 0:42












5 Answers
5






active

oldest

votes


















9














Whenever the Char's attributes are updated, the code needs to re-compute the HP.

All this sort of code is best put inside the Char object:



class Char:
def __init__(self, x, y):
self.str = x
self.con = y
self.setHP()

def __str__(self):
text = "strength: " + str(self.str) + "n"
"constitution: " + str(self.con) + "n"
"hp: " + str(self.hp)
return text

def setHP(self):
self.hp = (self.con + self.str) / 2

def adjustStr(self, amount):
self.str += amount
self.setHP()

def adjustCon(self, amount):
self.con += amount
self.setHP()


def main(dude):
print(str(dude))
print("------")
action = input("press 1 to change str, 2 to change con")
if action == "1":
dude.adjustStr(10)
main(dude)
elif action == "2":
dude.adjustCon(10)
main(dude)
else:
main(dude)


player = Char(20, 20)

main(player)





share|improve this answer
























  • clever way to do it, and without the decorator headache

    – SuperStew
    Dec 19 '18 at 0:47











  • @Kingsley This method is great! thank you so much. Very easy to implement. And thank you for the idea of adding "def __str__(self):"

    – Dominic DiTaranto
    Dec 19 '18 at 0:55



















2














The hp attribute does not change when the str or con change. The only time it is set is in the constructor. You could define an update method to Char like this:



class Char:
def __init__(self, x, y):
self.str = x
self.con = y
self.update()
def update(self):
self.hp = (self.con + self.str) / 2


and call it at the end of main:



def main(dude):
print("strength: " + str(dude.str))
print("constitution: " + str(dude.con))
print("hp: " + str(dude.hp))
print("------")
action = input("press 1 to change str, 2 to change con")
if action == "1":
dude.str = dude.str + 10
main(dude)
elif action == "2":
dude.con = dude.con + 10
main(dude)
else:
main(dude)
dude.update()





share|improve this answer































    1














    Because you evaluate the hp attribute only in the __init__() method, i. e. only in your statement



    player = Char(20, 20)


    The most quick fix (not a very nice one, but a very simple) is to create a new instance of Char after each change:



    if action == "1":
    dude.str = dude.str + 10
    dude = Char(dude.str, dude.con) # new dude (with current str and con)
    main(dude)
    elif action == "2":
    dude.con = dude.con + 10
    dude = Char(dude.str, dude.con) # new dude (with current str and con)
    main(dude)





    share|improve this answer


























    • @DominicDiTaranto, I added a hotfix into my answer.

      – MarianD
      Dec 19 '18 at 0:51



















    1














    You have to update the hp each time, since you used self.str and self.con only once to compute hp and stored it independently from self.str and self.con. You could use a get_hp for that.



    class Char:

    def __init__(self, x, y):
    self.str = x
    self.con = y

    def get_hp(self):
    return (self.con + self.str) / 2

    player = Char(20, 20)


    def main(dude):
    print("strength: " + str(dude.str))
    print("constitution: " + str(dude.con))
    print("hp: " + str(dude.get_hp()))
    print("------")
    action = input("press 1 to change str, 2 to change con")
    if action == "1":
    dude.str = dude.str + 10
    main(dude)
    elif action == "2":
    dude.con = dude.con + 10
    main(dude)
    else:
    main(dude)





    share|improve this answer

































      0














      I don't agree with the other answers, as the best way for me would have been to use a property. Properties work just like getters, but has the benefit to allow it to be dynamically calculated, which is exactly what you are looking for.



      class Char:

      def __init__(self, x, y):
      self.str = x
      self.con = y

      @property
      def hp(self):
      # todo handle negative hp!
      return (self.con + self.str) / 2.


      def test_hp():
      player = Char(20, 20)
      assert player.hp == 20


      def test_hp_with_changes_to_con_or_str():
      player = Char(20, 20)
      player.con += 10
      assert player.hp == 25
      player.str += 10
      assert player.hp == 30


      Also, as you can see, it is better to just write a test than to test stuff with output to stdout.



      scratch_12.py ..                                                         [100%]

      ========================== 2 passed in 0.20 seconds ===========================


      The above hp property is read-only, but you can make a setter for it as well if needed. Read up on property



      PS: also, use full names, like Character as it is better to be explicit than implicit :)






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53843061%2fvariable-x-not-updating-when-variables-that-should-effect-x-change%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        9














        Whenever the Char's attributes are updated, the code needs to re-compute the HP.

        All this sort of code is best put inside the Char object:



        class Char:
        def __init__(self, x, y):
        self.str = x
        self.con = y
        self.setHP()

        def __str__(self):
        text = "strength: " + str(self.str) + "n"
        "constitution: " + str(self.con) + "n"
        "hp: " + str(self.hp)
        return text

        def setHP(self):
        self.hp = (self.con + self.str) / 2

        def adjustStr(self, amount):
        self.str += amount
        self.setHP()

        def adjustCon(self, amount):
        self.con += amount
        self.setHP()


        def main(dude):
        print(str(dude))
        print("------")
        action = input("press 1 to change str, 2 to change con")
        if action == "1":
        dude.adjustStr(10)
        main(dude)
        elif action == "2":
        dude.adjustCon(10)
        main(dude)
        else:
        main(dude)


        player = Char(20, 20)

        main(player)





        share|improve this answer
























        • clever way to do it, and without the decorator headache

          – SuperStew
          Dec 19 '18 at 0:47











        • @Kingsley This method is great! thank you so much. Very easy to implement. And thank you for the idea of adding "def __str__(self):"

          – Dominic DiTaranto
          Dec 19 '18 at 0:55
















        9














        Whenever the Char's attributes are updated, the code needs to re-compute the HP.

        All this sort of code is best put inside the Char object:



        class Char:
        def __init__(self, x, y):
        self.str = x
        self.con = y
        self.setHP()

        def __str__(self):
        text = "strength: " + str(self.str) + "n"
        "constitution: " + str(self.con) + "n"
        "hp: " + str(self.hp)
        return text

        def setHP(self):
        self.hp = (self.con + self.str) / 2

        def adjustStr(self, amount):
        self.str += amount
        self.setHP()

        def adjustCon(self, amount):
        self.con += amount
        self.setHP()


        def main(dude):
        print(str(dude))
        print("------")
        action = input("press 1 to change str, 2 to change con")
        if action == "1":
        dude.adjustStr(10)
        main(dude)
        elif action == "2":
        dude.adjustCon(10)
        main(dude)
        else:
        main(dude)


        player = Char(20, 20)

        main(player)





        share|improve this answer
























        • clever way to do it, and without the decorator headache

          – SuperStew
          Dec 19 '18 at 0:47











        • @Kingsley This method is great! thank you so much. Very easy to implement. And thank you for the idea of adding "def __str__(self):"

          – Dominic DiTaranto
          Dec 19 '18 at 0:55














        9












        9








        9







        Whenever the Char's attributes are updated, the code needs to re-compute the HP.

        All this sort of code is best put inside the Char object:



        class Char:
        def __init__(self, x, y):
        self.str = x
        self.con = y
        self.setHP()

        def __str__(self):
        text = "strength: " + str(self.str) + "n"
        "constitution: " + str(self.con) + "n"
        "hp: " + str(self.hp)
        return text

        def setHP(self):
        self.hp = (self.con + self.str) / 2

        def adjustStr(self, amount):
        self.str += amount
        self.setHP()

        def adjustCon(self, amount):
        self.con += amount
        self.setHP()


        def main(dude):
        print(str(dude))
        print("------")
        action = input("press 1 to change str, 2 to change con")
        if action == "1":
        dude.adjustStr(10)
        main(dude)
        elif action == "2":
        dude.adjustCon(10)
        main(dude)
        else:
        main(dude)


        player = Char(20, 20)

        main(player)





        share|improve this answer













        Whenever the Char's attributes are updated, the code needs to re-compute the HP.

        All this sort of code is best put inside the Char object:



        class Char:
        def __init__(self, x, y):
        self.str = x
        self.con = y
        self.setHP()

        def __str__(self):
        text = "strength: " + str(self.str) + "n"
        "constitution: " + str(self.con) + "n"
        "hp: " + str(self.hp)
        return text

        def setHP(self):
        self.hp = (self.con + self.str) / 2

        def adjustStr(self, amount):
        self.str += amount
        self.setHP()

        def adjustCon(self, amount):
        self.con += amount
        self.setHP()


        def main(dude):
        print(str(dude))
        print("------")
        action = input("press 1 to change str, 2 to change con")
        if action == "1":
        dude.adjustStr(10)
        main(dude)
        elif action == "2":
        dude.adjustCon(10)
        main(dude)
        else:
        main(dude)


        player = Char(20, 20)

        main(player)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 19 '18 at 0:45









        KingsleyKingsley

        2,30211123




        2,30211123













        • clever way to do it, and without the decorator headache

          – SuperStew
          Dec 19 '18 at 0:47











        • @Kingsley This method is great! thank you so much. Very easy to implement. And thank you for the idea of adding "def __str__(self):"

          – Dominic DiTaranto
          Dec 19 '18 at 0:55



















        • clever way to do it, and without the decorator headache

          – SuperStew
          Dec 19 '18 at 0:47











        • @Kingsley This method is great! thank you so much. Very easy to implement. And thank you for the idea of adding "def __str__(self):"

          – Dominic DiTaranto
          Dec 19 '18 at 0:55

















        clever way to do it, and without the decorator headache

        – SuperStew
        Dec 19 '18 at 0:47





        clever way to do it, and without the decorator headache

        – SuperStew
        Dec 19 '18 at 0:47













        @Kingsley This method is great! thank you so much. Very easy to implement. And thank you for the idea of adding "def __str__(self):"

        – Dominic DiTaranto
        Dec 19 '18 at 0:55





        @Kingsley This method is great! thank you so much. Very easy to implement. And thank you for the idea of adding "def __str__(self):"

        – Dominic DiTaranto
        Dec 19 '18 at 0:55













        2














        The hp attribute does not change when the str or con change. The only time it is set is in the constructor. You could define an update method to Char like this:



        class Char:
        def __init__(self, x, y):
        self.str = x
        self.con = y
        self.update()
        def update(self):
        self.hp = (self.con + self.str) / 2


        and call it at the end of main:



        def main(dude):
        print("strength: " + str(dude.str))
        print("constitution: " + str(dude.con))
        print("hp: " + str(dude.hp))
        print("------")
        action = input("press 1 to change str, 2 to change con")
        if action == "1":
        dude.str = dude.str + 10
        main(dude)
        elif action == "2":
        dude.con = dude.con + 10
        main(dude)
        else:
        main(dude)
        dude.update()





        share|improve this answer




























          2














          The hp attribute does not change when the str or con change. The only time it is set is in the constructor. You could define an update method to Char like this:



          class Char:
          def __init__(self, x, y):
          self.str = x
          self.con = y
          self.update()
          def update(self):
          self.hp = (self.con + self.str) / 2


          and call it at the end of main:



          def main(dude):
          print("strength: " + str(dude.str))
          print("constitution: " + str(dude.con))
          print("hp: " + str(dude.hp))
          print("------")
          action = input("press 1 to change str, 2 to change con")
          if action == "1":
          dude.str = dude.str + 10
          main(dude)
          elif action == "2":
          dude.con = dude.con + 10
          main(dude)
          else:
          main(dude)
          dude.update()





          share|improve this answer


























            2












            2








            2







            The hp attribute does not change when the str or con change. The only time it is set is in the constructor. You could define an update method to Char like this:



            class Char:
            def __init__(self, x, y):
            self.str = x
            self.con = y
            self.update()
            def update(self):
            self.hp = (self.con + self.str) / 2


            and call it at the end of main:



            def main(dude):
            print("strength: " + str(dude.str))
            print("constitution: " + str(dude.con))
            print("hp: " + str(dude.hp))
            print("------")
            action = input("press 1 to change str, 2 to change con")
            if action == "1":
            dude.str = dude.str + 10
            main(dude)
            elif action == "2":
            dude.con = dude.con + 10
            main(dude)
            else:
            main(dude)
            dude.update()





            share|improve this answer













            The hp attribute does not change when the str or con change. The only time it is set is in the constructor. You could define an update method to Char like this:



            class Char:
            def __init__(self, x, y):
            self.str = x
            self.con = y
            self.update()
            def update(self):
            self.hp = (self.con + self.str) / 2


            and call it at the end of main:



            def main(dude):
            print("strength: " + str(dude.str))
            print("constitution: " + str(dude.con))
            print("hp: " + str(dude.hp))
            print("------")
            action = input("press 1 to change str, 2 to change con")
            if action == "1":
            dude.str = dude.str + 10
            main(dude)
            elif action == "2":
            dude.con = dude.con + 10
            main(dude)
            else:
            main(dude)
            dude.update()






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 19 '18 at 0:46









            pizza static void mainpizza static void main

            1,6321924




            1,6321924























                1














                Because you evaluate the hp attribute only in the __init__() method, i. e. only in your statement



                player = Char(20, 20)


                The most quick fix (not a very nice one, but a very simple) is to create a new instance of Char after each change:



                if action == "1":
                dude.str = dude.str + 10
                dude = Char(dude.str, dude.con) # new dude (with current str and con)
                main(dude)
                elif action == "2":
                dude.con = dude.con + 10
                dude = Char(dude.str, dude.con) # new dude (with current str and con)
                main(dude)





                share|improve this answer


























                • @DominicDiTaranto, I added a hotfix into my answer.

                  – MarianD
                  Dec 19 '18 at 0:51
















                1














                Because you evaluate the hp attribute only in the __init__() method, i. e. only in your statement



                player = Char(20, 20)


                The most quick fix (not a very nice one, but a very simple) is to create a new instance of Char after each change:



                if action == "1":
                dude.str = dude.str + 10
                dude = Char(dude.str, dude.con) # new dude (with current str and con)
                main(dude)
                elif action == "2":
                dude.con = dude.con + 10
                dude = Char(dude.str, dude.con) # new dude (with current str and con)
                main(dude)





                share|improve this answer


























                • @DominicDiTaranto, I added a hotfix into my answer.

                  – MarianD
                  Dec 19 '18 at 0:51














                1












                1








                1







                Because you evaluate the hp attribute only in the __init__() method, i. e. only in your statement



                player = Char(20, 20)


                The most quick fix (not a very nice one, but a very simple) is to create a new instance of Char after each change:



                if action == "1":
                dude.str = dude.str + 10
                dude = Char(dude.str, dude.con) # new dude (with current str and con)
                main(dude)
                elif action == "2":
                dude.con = dude.con + 10
                dude = Char(dude.str, dude.con) # new dude (with current str and con)
                main(dude)





                share|improve this answer















                Because you evaluate the hp attribute only in the __init__() method, i. e. only in your statement



                player = Char(20, 20)


                The most quick fix (not a very nice one, but a very simple) is to create a new instance of Char after each change:



                if action == "1":
                dude.str = dude.str + 10
                dude = Char(dude.str, dude.con) # new dude (with current str and con)
                main(dude)
                elif action == "2":
                dude.con = dude.con + 10
                dude = Char(dude.str, dude.con) # new dude (with current str and con)
                main(dude)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 19 '18 at 0:54

























                answered Dec 19 '18 at 0:42









                MarianDMarianD

                4,28761331




                4,28761331













                • @DominicDiTaranto, I added a hotfix into my answer.

                  – MarianD
                  Dec 19 '18 at 0:51



















                • @DominicDiTaranto, I added a hotfix into my answer.

                  – MarianD
                  Dec 19 '18 at 0:51

















                @DominicDiTaranto, I added a hotfix into my answer.

                – MarianD
                Dec 19 '18 at 0:51





                @DominicDiTaranto, I added a hotfix into my answer.

                – MarianD
                Dec 19 '18 at 0:51











                1














                You have to update the hp each time, since you used self.str and self.con only once to compute hp and stored it independently from self.str and self.con. You could use a get_hp for that.



                class Char:

                def __init__(self, x, y):
                self.str = x
                self.con = y

                def get_hp(self):
                return (self.con + self.str) / 2

                player = Char(20, 20)


                def main(dude):
                print("strength: " + str(dude.str))
                print("constitution: " + str(dude.con))
                print("hp: " + str(dude.get_hp()))
                print("------")
                action = input("press 1 to change str, 2 to change con")
                if action == "1":
                dude.str = dude.str + 10
                main(dude)
                elif action == "2":
                dude.con = dude.con + 10
                main(dude)
                else:
                main(dude)





                share|improve this answer






























                  1














                  You have to update the hp each time, since you used self.str and self.con only once to compute hp and stored it independently from self.str and self.con. You could use a get_hp for that.



                  class Char:

                  def __init__(self, x, y):
                  self.str = x
                  self.con = y

                  def get_hp(self):
                  return (self.con + self.str) / 2

                  player = Char(20, 20)


                  def main(dude):
                  print("strength: " + str(dude.str))
                  print("constitution: " + str(dude.con))
                  print("hp: " + str(dude.get_hp()))
                  print("------")
                  action = input("press 1 to change str, 2 to change con")
                  if action == "1":
                  dude.str = dude.str + 10
                  main(dude)
                  elif action == "2":
                  dude.con = dude.con + 10
                  main(dude)
                  else:
                  main(dude)





                  share|improve this answer




























                    1












                    1








                    1







                    You have to update the hp each time, since you used self.str and self.con only once to compute hp and stored it independently from self.str and self.con. You could use a get_hp for that.



                    class Char:

                    def __init__(self, x, y):
                    self.str = x
                    self.con = y

                    def get_hp(self):
                    return (self.con + self.str) / 2

                    player = Char(20, 20)


                    def main(dude):
                    print("strength: " + str(dude.str))
                    print("constitution: " + str(dude.con))
                    print("hp: " + str(dude.get_hp()))
                    print("------")
                    action = input("press 1 to change str, 2 to change con")
                    if action == "1":
                    dude.str = dude.str + 10
                    main(dude)
                    elif action == "2":
                    dude.con = dude.con + 10
                    main(dude)
                    else:
                    main(dude)





                    share|improve this answer















                    You have to update the hp each time, since you used self.str and self.con only once to compute hp and stored it independently from self.str and self.con. You could use a get_hp for that.



                    class Char:

                    def __init__(self, x, y):
                    self.str = x
                    self.con = y

                    def get_hp(self):
                    return (self.con + self.str) / 2

                    player = Char(20, 20)


                    def main(dude):
                    print("strength: " + str(dude.str))
                    print("constitution: " + str(dude.con))
                    print("hp: " + str(dude.get_hp()))
                    print("------")
                    action = input("press 1 to change str, 2 to change con")
                    if action == "1":
                    dude.str = dude.str + 10
                    main(dude)
                    elif action == "2":
                    dude.con = dude.con + 10
                    main(dude)
                    else:
                    main(dude)






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 19 '18 at 0:59

























                    answered Dec 19 '18 at 0:53









                    guruguru

                    15011




                    15011























                        0














                        I don't agree with the other answers, as the best way for me would have been to use a property. Properties work just like getters, but has the benefit to allow it to be dynamically calculated, which is exactly what you are looking for.



                        class Char:

                        def __init__(self, x, y):
                        self.str = x
                        self.con = y

                        @property
                        def hp(self):
                        # todo handle negative hp!
                        return (self.con + self.str) / 2.


                        def test_hp():
                        player = Char(20, 20)
                        assert player.hp == 20


                        def test_hp_with_changes_to_con_or_str():
                        player = Char(20, 20)
                        player.con += 10
                        assert player.hp == 25
                        player.str += 10
                        assert player.hp == 30


                        Also, as you can see, it is better to just write a test than to test stuff with output to stdout.



                        scratch_12.py ..                                                         [100%]

                        ========================== 2 passed in 0.20 seconds ===========================


                        The above hp property is read-only, but you can make a setter for it as well if needed. Read up on property



                        PS: also, use full names, like Character as it is better to be explicit than implicit :)






                        share|improve this answer




























                          0














                          I don't agree with the other answers, as the best way for me would have been to use a property. Properties work just like getters, but has the benefit to allow it to be dynamically calculated, which is exactly what you are looking for.



                          class Char:

                          def __init__(self, x, y):
                          self.str = x
                          self.con = y

                          @property
                          def hp(self):
                          # todo handle negative hp!
                          return (self.con + self.str) / 2.


                          def test_hp():
                          player = Char(20, 20)
                          assert player.hp == 20


                          def test_hp_with_changes_to_con_or_str():
                          player = Char(20, 20)
                          player.con += 10
                          assert player.hp == 25
                          player.str += 10
                          assert player.hp == 30


                          Also, as you can see, it is better to just write a test than to test stuff with output to stdout.



                          scratch_12.py ..                                                         [100%]

                          ========================== 2 passed in 0.20 seconds ===========================


                          The above hp property is read-only, but you can make a setter for it as well if needed. Read up on property



                          PS: also, use full names, like Character as it is better to be explicit than implicit :)






                          share|improve this answer


























                            0












                            0








                            0







                            I don't agree with the other answers, as the best way for me would have been to use a property. Properties work just like getters, but has the benefit to allow it to be dynamically calculated, which is exactly what you are looking for.



                            class Char:

                            def __init__(self, x, y):
                            self.str = x
                            self.con = y

                            @property
                            def hp(self):
                            # todo handle negative hp!
                            return (self.con + self.str) / 2.


                            def test_hp():
                            player = Char(20, 20)
                            assert player.hp == 20


                            def test_hp_with_changes_to_con_or_str():
                            player = Char(20, 20)
                            player.con += 10
                            assert player.hp == 25
                            player.str += 10
                            assert player.hp == 30


                            Also, as you can see, it is better to just write a test than to test stuff with output to stdout.



                            scratch_12.py ..                                                         [100%]

                            ========================== 2 passed in 0.20 seconds ===========================


                            The above hp property is read-only, but you can make a setter for it as well if needed. Read up on property



                            PS: also, use full names, like Character as it is better to be explicit than implicit :)






                            share|improve this answer













                            I don't agree with the other answers, as the best way for me would have been to use a property. Properties work just like getters, but has the benefit to allow it to be dynamically calculated, which is exactly what you are looking for.



                            class Char:

                            def __init__(self, x, y):
                            self.str = x
                            self.con = y

                            @property
                            def hp(self):
                            # todo handle negative hp!
                            return (self.con + self.str) / 2.


                            def test_hp():
                            player = Char(20, 20)
                            assert player.hp == 20


                            def test_hp_with_changes_to_con_or_str():
                            player = Char(20, 20)
                            player.con += 10
                            assert player.hp == 25
                            player.str += 10
                            assert player.hp == 30


                            Also, as you can see, it is better to just write a test than to test stuff with output to stdout.



                            scratch_12.py ..                                                         [100%]

                            ========================== 2 passed in 0.20 seconds ===========================


                            The above hp property is read-only, but you can make a setter for it as well if needed. Read up on property



                            PS: also, use full names, like Character as it is better to be explicit than implicit :)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 23 '18 at 23:03









                            TjorriemorrieTjorriemorrie

                            8,1741164104




                            8,1741164104






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53843061%2fvariable-x-not-updating-when-variables-that-should-effect-x-change%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Plaza Victoria

                                In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                                How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...