Modifying Classes - 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 Classes (/thread-20267.html) |
Modifying Classes - Tridium - Aug-02-2019 Hello, I am trying to better understand Classes. This time, I'm trying to modify Class attributes(? I think this is the correct term) using a function. Anyone got an explanation why this method isn't working, and any tips on how to get back on track? #Classes #Functions class Player(): def __init__(self, Name, Strength = 0, Constitution = 0, Intelligence = 0, Health = 0, Mana = 0, Attack = 0): self.Name = name self.str = Strength self.con = Constitution self.int = Intelligence self.hp = Health self.mp = Mana self.Attack = Attack def character_creation(Player): points_left = 10 name = input('What do they call you?: ') Player.name = name print('Welcome {}, Make your Character!'.format(Player.name)) Player.Strength = Playerstr while points_left > 0: print('1: Add point in Strength') print('2: Add point in Constitution') print('3: Add point in Intelligence') choice = input('Add 1 to a skill: ') points_left -= 1 print('You have {} points left!'.format(points_left)) if choice == '1': Playerstr += 1 print(Player.Strength) name = input('What do they call you?: ') Player.name = name print('Welcome {}, Make your Character!'.format(Player.name)) character_creation(Player)So, when I'm fumbling around with this, I usually get two types of errors when I mess with my code: When I remove line 19, I get this error: I believe these errors may have to do with instance vs global I tried for a while to play with the codes: class Player: def __init__(self, Name = '', Strength = 0, Constitution = 0, Intelligence = 0): self.Name = Name self.Strength = Strength self.Constitution = Constitution self.Intelligence = Intelligence def character_creation(Player): points_left = 10 while points_left > 0: print('1: Add point in Strength') print('2: Add point in Constitution') print('3: Add point in Intelligence') choice = input('Add 1 to a skill: ') if choice == '1': Player = Player(0, 1, 0, 0) points_left -= 1 # Playerstr += 1 print('Strength: ', Player.Strength) print('You have {} points left!'.format(points_left)) else: pass character_creation(Player)I found it updates the strength value once and then fails on the next attempt.
RE: Modifying Classes - SheeppOSU - Aug-02-2019 I'm not getting errors. I also want to point out how your code could be shortened down to this rather than using a bunch of if statements. class Player: def __init__(self, stats, Name = ''): self.Name = Name self.Strength = stats['Strength'] self.Constitution = stats['Constitution'] self.Intelligence = stats['Intelligence'] def character_creation(Player): statsDict = {'Strength' : 0, 'Constitution' : 0, 'Intelligence' : 0} ##Store skill pts PointDict = {'1' : 'Strength', '2' : 'Constitution', '3' : 'Intelligence'} #Use number to determine the skill points_left = 10 while points_left > 0: print('1: Add point in Strength') print('2: Add point in Constitution') print('3: Add point in Intelligence') choice = input('Add 1 to a skill: ') while choice not in PointDict: #Make sure they're answer is valid choice = input('Add 1 to a skill: ') statsDict[PointDict[choice]] += 1 #Add a skill point points_left -= 1 #Take away a pt print('%s: %s' %(PointDict[choice], statsDict[PointDict[choice]])) print('You have %s points left!' %points_left) return Player(stats=statsDict) #After the while loop return a player. Give the player the whole stats dictionary is easier character_creation(Player) RE: Modifying Classes - ndc85430 - Aug-03-2019 (Aug-02-2019, 11:44 PM)Tridium Wrote: This error is self-explanatory. What are you having difficulty understanding here? Quote:I believe these errors may have to do with instance vs global The problems here occur because you aren't passing an instance of Player to the function, you're passing the type object itself. Line 17 in that code would be a valid way to call __init__ to create a new instance if you weren't passing the type object (though even if you had passed an instance, why you'd continually want to overwrite it with an instance with the same values (rather than just setting the attributes accordingly) is not obvious to me).Perhaps you need to go and read about classes and how to instantiate them again? RE: Modifying Classes - Windspar - Aug-03-2019 Explain classes on my site. Quick class run down. self refer to class object. Class object must be created. player = Player(...) # create a new player object player2 = Player(...) # create another new player objectplayer and player2 are to different objects. If your function effects class only. Then make it part of the class. Example #Class class Player(): def __init__(self, name="", strength=0, constitution=0, intelligence=0, health=0, mana=0, attack=0): self.name = name self.str = strength self.con = constitution self.int = intelligence self.hp = health self.mp = mana self.attack = attack # Allows to print and represent it self in other areas. def __repr__(self): return str(vars(self)) # Method def creation(self): name = input('What do they call you?: ') self.name = name print('Welcome {}, Make your Character!'.format(self.name)) points = 10 for points_left in range(points): print('1: Add point in Strength') print('2: Add point in Constitution') print('3: Add point in Intelligence') retries = 0 while True: choice = input('Add 1 to a skill: ') choice = choice.strip() if choice in ['1', '2', '3']: break print('Try Again !') retries += 1 if retries > 3: print('You having to much trouble. Seek assistent.\nGood Bye') exit() print('You have {} points left!'.format(points - points_left - 1)) if choice == '1': self.str += 1 print("strength:", self.str) elif choice == '2': self.con += 1 print("constitution:", self.con) elif choice == '3': self.int += 1 print("intelligence:", self.int) def main(): player = Player() player.creation() print(player) main() Couple of other tips. Try avoid keyword names. Try to follow style guide. RE: Modifying Classes - Tridium - Aug-03-2019 Thank you to everyone who replied here. I appreciate all the answers I've received and hope to learn from them and implement them into my project. I chose this project as the method I am going to teach myself python so, sadly, a lot of the problems and errors I've encountered aren't obvious to me, but I am improving! |