I cannot use functions and search for class attribute of an object

Asked

Viewed 26 times

-1

I’m not being able to call the attributes of the objects I create by the create_character function or activate the functions belonging to the class. I think when I try to assign the object in the characterList list it is losing some properties because I can’t even call . name it. someone could help me to see what is wrong or missing?

from dataclasses import dataclass,fields,MISSING




characterList = [""]

@dataclass
class character:
    name : str
    genre : str
    race : str
    health : int = 0
    recoveryRate : int = 0
    strength : int = 0
    agility : int = 0
    intellect : int = 0
    will : int = 0
    perception : int = 0
    defense : int = 0
    size : int = 0
    movement : int = 0
    level : int = 0
    power : int = 0
    damage : int = 0
    insanity : int = 0
    corruption : int = 0


    def updateByRace(self):
        if self.race == "human":
            self.strength = 10
            self.agility = 10
            self.intellect = 10
            self.will = 10

            self.perception = self.intellect
            self.defens = self.agility
            self.health = self.strength
            self.recoveryRate = (self.health)/4
            self.size = 1
            self.movement = 10


    def __str__(self):
        return(
            'Character Sheet: \n\n'
            f'Name : {self.name} | '
            f'Genre : {self.genre} | '
            f'Race : {self.race} \n'
            f'Level : {self.level} |'
            f'Power : {self.power} \n'
            f'Damage : {self.damage} |'
            f'Health : {self.health} | '
            f'recoveryRate : {self.recoveryRate} \n'
            f'STR : {self.strength} | '
            f'INT : {self.intellect} | '
            f'AGI : {self.agility} | '
            f'Will : {self.will} \n'
            f'Perception : {self.perception} | '
            f'Defense : {self.defense} | '
            f'Movement : {self.movement} | \n'
            f'Size : {self.size} | '
            f'Insanity : {self.insanity} | '
            f'Corruption : {self.corruption} \n'

        )






def create_character():
    print('New character:')
    global chacterList
    info = {}
    s = True
    while s:
        for field in fields(character):
            if field.default == MISSING:
                value = input(f'{field.name}: ')
                info[field.name] = field.type(value)

        characterList.append(character)
        x = input("do you want to add a new character? [y/n]: ")
        if  x != "y" :
            s = False



create_character()
print(characterList)
print(characterList[1].name)
  • characterList.append(character), you are adding the class itself to the list, you have not created the instance of the class with info.

  • And it got really weird that you started the list characterList with a string empty. Why did you do it?

  • was doing this for the convenience of starting at 1, but only for testing even at the end would remove to be able to go through the list at display all.

1 answer

2

The prblema is that in your code, at no point do you create a new object from class character - You gather information from the fields in a dictionary called info, and ignore this questionnaire, and your list you add the class itself, not objects of that class

Modify the section below:


  while s:
        for field in fields(character):
            if field.default == MISSING:
                value = input(f'{field.name}: ')
                info[field.name] = field.type(value)

        characterList.append(character)
        x = input("do you want to add a new character? [y/n]: ")
        if  x != "y" :
            s = False

For this - which includes the creation of a new instance of character:


  while s:
        info = {} 
        for field in fields(character):
            if field.default == MISSING:
                value = input(f'{field.name}: ')
                info[field.name] = field.type(value)

        new_character = character(**info)

        characterList.append(new_character)
        x = input("do you want to add a new character? [y/n]: ")
        if  x != "y" :
            s = False

  • before I was using Character(**info) but in Return, I thought that as it displayed the whole object was a type of print, gave now here mt obg same!

  • @pydoni In other code displayed why function return was used as function parameter print.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.