-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 withinfo
.– Woss
And it got really weird that you started the list
characterList
with a string empty. Why did you do it?– Woss
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.
– pydoni