Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modifying Class values
#3
(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.

You want to change line 32 to self.level += 1, and delete line 30.

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)
Output:
{'Name': 'Dagger', 'level': 2, 'xp': 0, 'xpnextlvl': 25, 'Strength': 2, 'Constitution': 0, 'Endurance': -2, 'Dexterity': 0, 'Intelligence': -2, 'Wisdom': 0, 'Charisma': 2, 'Perception': 0, 'Luck': 0, 'armour': 0} {'Name': 'Greatsword', 'level': 2, 'xp': 0, 'xpnextlvl': 25, 'Strength': 4, 'Constitution': 2, 'Endurance': 2, 'Dexterity': -2, 'Intelligence': 0, 'Wisdom': 0, 'Charisma': 0, 'Perception': 0, 'Luck': 0, 'armour': 0} {'Name': 'Breast Plate', 'level': 1, 'xp': 0, 'xpnextlvl': 25, 'Strength': 0, 'Constitution': 0, 'Endurance': 0, 'Dexterity': 0, 'Intelligence': 0, 'Wisdom': 0, 'Charisma': 0, 'Perception': 0, 'Luck': 0, 'armour': 5}
Reply


Messages In This Thread
Modifying Class values - by Tridium - Aug-05-2019, 09:23 PM
RE: Modifying Class values - by ichabod801 - Aug-05-2019, 09:52 PM
RE: Modifying Class values - by Tridium - Aug-05-2019, 10:29 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020