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

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
Reply


Messages In This Thread
Modifying Classes - by Tridium - Aug-02-2019, 11:44 PM
RE: Modifying Classes - by SheeppOSU - Aug-02-2019, 11:59 PM
RE: Modifying Classes - by ndc85430 - Aug-03-2019, 06:06 AM
RE: Modifying Classes - by Windspar - Aug-03-2019, 03:42 PM
RE: Modifying Classes - by Tridium - Aug-03-2019, 05:41 PM

Forum Jump:

User Panel Messages

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