Aug-03-2019, 06:06 AM
(Aug-02-2019, 11:44 PM)Tridium Wrote:
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
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
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 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
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?