Aug-02-2019, 11:44 PM
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?
I tried for a while to play with the codes:
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:
Error:Traceback (most recent call last):
File "c:/Users/Tridium/Documents/python/python codes/classes.py", line 38, in <module>
character_creation(Player)
File "c:/Users/Tridium/Documents/python/python codes/classes.py", line 19, in character_creation
Player.Strength = Playerstr
UnboundLocalError: local variable 'Playerstr' referenced before assignment
When I remove line 19, I get this error:Error:Traceback (most recent call last):
File "c:/Users/Tridium/Documents/python/python codes/classes.py", line 37, in <module>
character_creation(Player)
File "c:/Users/Tridium/Documents/python/python codes/classes.py", line 30, in character_creation
Player.Strength += 1
AttributeError: type object 'Player' has no attribute 'Strength'
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.
Error:Traceback (most recent call last):
File "c:/Users/Tridium/Documents/python/python codes/classes.py", line 28, in <module>
character_creation(Player)
File "c:/Users/Tridium/Documents/python/python codes/classes.py", line 19, in character_creation
Player = Player(0, 1, 0, 0)
TypeError: 'Player' object is not callable