Modifying Class values - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Modifying Class values (/thread-20338.html) |
Modifying Class values - Tridium - Aug-05-2019 Hello, again... You guys recently replied to my post: https://python-forum.io/Thread-Modifying-Classes, and it was of great help to me I was able to implement it and expand it for what I needed, So thank you, that was awesome. But, recently, I have come across a similar problem and I've spent a lot of time researching and trying to fix it, I was hoping you guys might be able to explain to me what I'm doing wrong. My goal is to use a function to increase the level of 'dagger' by 1, but the function seems to have no effect at all on the result. My other goal is to multiply the attribute value by the level value but it doesn't seem to be updated with the class I created. Any advice would be greatly appreciated! class weapon(): def __init__(self, Name = '', level = 0, xp = 0, xpnextlvl = 25): self.Name = Name self.level = level self.xp = xp self.xpnextlvl = xpnextlvl def __repr__(self): return str(vars(self)) class attributes(): def __init__(self, Strength = 0, Constitution = 0, Endurance = 0, Dexterity = 0, Intelligence = 0, Wisdom = 0, Charisma = 0, Perception = 0, Luck = 0): self.Strength = Strength * weapon().level # Not updating 'Strength' Value by what I inputted as 'Dagger' self.Constitution = Constitution self.Endurance = Endurance self.Dexterity = Dexterity self.Intelligence = Intelligence self.Wisdom = Wisdom self.Charisma = Charisma self.Perception = Perception self.Luck = Luck def __repr__(self): return str(vars(self)) def weapon_level(self): #This function seem to have no effect on the values for 'Dagger' print('Welcome to the blacksmith') print('1: Level up') print('2: Quit') choice = input('choose: ') level = self.level if choice == '1': level += 1 dagger = weapon('Dagger', 1, 0 , 25), weapon.attributes(1, 1, 1, 1, 1, 1, 1, 1, 1) weapon.weapon_level(weapon(dagger)) print(dagger) The output should have level: 2 and strength: 2
RE: Modifying Class values - ichabod801 - Aug-05-2019 self.level is an integer, which is immutable. Immutable data types are assign by value, which mean a copy is made. So level has the same value as self.level after line 30, but level shares no connection to the value of self.level. So when you change the value of level on line 32, it has no effect on the value of self.level. You want to change line 32 to self.level += 1 , and delete line 30.
RE: Modifying Class values - Tridium - Aug-05-2019 (Aug-05-2019, 09:52 PM)ichabod801 Wrote: self.level is an integer, which is immutable. Immutable data types are assign by value, which mean a copy is made. So level has the same value as self.level after line 30, but level shares no connection to the value of self.level. So when you change the value of level on line 32, it has no effect on the value of self.level. Thank you, Ichabod. I was able to modify my code and achieve my desired results! class equipment(): def __init__(self, Name = '', level = 0, xp = 0, xpnextlvl = 25, Strength = 0, Constitution = 0, Endurance = 0, Dexterity = 0, Intelligence = 0, Wisdom = 0, Charisma = 0, Perception = 0, Luck = 0, armour = 0): self.Name = Name self.level = level self.xp = xp self.xpnextlvl = xpnextlvl self.Strength = Strength self.Constitution = Constitution self.Endurance = Endurance self.Dexterity = Dexterity self.Intelligence = Intelligence self.Wisdom = Wisdom self.Charisma = Charisma self.Perception = Perception self.Luck = Luck self.armour = armour def __repr__(self): return str(vars(self)) def equipment_level(self): self.level += 1 level = self.level self.Strength = self.Strength * level self.Constitution = self.Constitution * level self.Endurance = self.Endurance * level self.Dexterity = self.Dexterity * level self.Intelligence = self.Intelligence * level self.Wisdom = self.Wisdom * level self.Charisma = self.Charisma * level self.Perception = self.Perception * level self.Luck = self.Perception * level self.armour = self.armour * level class weapon(equipment): pass class armour(equipment): pass breast_plate = armour('Breast Plate', 1, 0, 25, armour = 5) dagger = weapon('Dagger', 1, 0 , 25, 1, 0, -1, 0, -1, 0, 1, 0, 1) Greatsword = weapon('Greatsword', 1, 0 , 25, 2, 1, 1, -1, 0, 0, 0, 0, 0) dagger.equipment_level() Greatsword.equipment_level() print(dagger) print(Greatsword) print(breast_plate)
|