Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Modifying Class values
#1
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)
Output:
Welcome to the blacksmith 1: Level up 2: Quit choose: 1 ({'Name': 'Dagger', 'level': 1, 'xp': 0, 'xpnextlvl': 25}, {'Strength': 0, 'Constitution': 1, 'Endurance': 1, 'Dexterity': 1, 'Intelligence': 1, 'Wisdom': 1, 'Charisma': 1, 'Perception': 1, 'Luck': 1})
The output should have level: 2 and strength: 2
Quote
#2
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.
Tridium likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#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}
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)