I'm a student currently learning Python and was wondering how the question in the title could be solved. For a project, I'm creating a game that uses a class to store certain variables for different characters. Below is a portion of the class:
class character(object):
def __init__(self, vitality):
self.vitality = vitality
self.stats = f'Vitality: {self.vitality}'
I haven't included the full class as I have like 41 variables in it, some of which are based on a combination of the others, but this is the gist of it.
Later in the code, I let the user choose a variable to increment by 1 in order to level up:
levelUpChoice = ''
levelUpChoiceList = ['1', '2', '3', '4', '5', '6', '7']
while levelUpChoice == '':
levelUpChoice = input('What stat would you like to increase?\n1) Vitality\n2) Endurance\n3) Strength\n4) Dexterity\n5) Wisdom\n6) Faith\n7) Luck\n\n > ')
if levelUpChoice not in levelUpChoiceList:
levelUpChoice = ''
print('That is not an accepted response!\nUse a number 1-7.')
if int(levelUpChoice) == 1:
charClass.vitality += 1
However, the class variable called "stats" doesn't get updated when I increment "vitality" by 1, even though it's based on it. As I literally learned about classes and how to generally use them 2 days ago, I'm not quite sure how to deal with this, but I need a solution for the project to have full functionality. Seeing as my most important class variables depend on the basic ones I plan on changing, I really need a solution.
Any tips or suggestions would be greatly appreciated.