Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable X not updating when variables that should effect X change

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!

like image 969
Dominic DiTaranto Avatar asked Dec 19 '18 00:12

Dominic DiTaranto


People also ask

How do you update the value of a variable?

Updating a variable by adding something to it is called an increment; subtracting is called a decrement. Sometimes programmers talk about incrementing or decrementing without specifying by how much; when they do they usually mean by 1.

How do you update a variable in Javascript?

Updating Variables We can update our variables in shorter ways by using the += , *= , -= or /= operators. This way, we don't have to repeat the variable name when we assign them to something. Then we increase the value of i by 2. If we increment or decrement only by 1, then we can use ++ or -- respectively.

Can a function change a global variable?

A local variable is a variable defined within a function. Such variables are said to have local 'scope'. Functions can access global variables and modify them. Modifying global variables in a function is considered poor programming practice.

How do you update a global variable in python?

Use of “global†keyword to modify global variable inside a function. If your function has a local variable with same name as global variable and you want to modify the global variable inside function then use 'global' keyword before the variable name at start of function i.e.


3 Answers

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)
like image 136
Kingsley Avatar answered Nov 15 '22 13:11

Kingsley


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()
like image 38
Pika Supports Ukraine Avatar answered Nov 15 '22 15:11

Pika Supports Ukraine


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)
like image 24
MarianD Avatar answered Nov 15 '22 15:11

MarianD