Home > Blockchain >  How do I affect the contents of multiple dictionaries or objects iteratively from within a loop?
How do I affect the contents of multiple dictionaries or objects iteratively from within a loop?

Time:02-28

I'm trying to use a pen-and-paper RPG system I know as a springboard for learning Python. I want to be able to use an object to represent a character, with all the stats that implies. I'm trying to use for and while loops to do things like determine number of actions per turn (based on individual speed) and the actions they take. My problem is that I can't figure out how to refer to the object within the loop so that Python goes through and sequentially affects each of the objects the way I'd want.

class char:            
    counter = 0         #char counter to keep track of the nbr of doodz
    Asidecounter = 0
    Bsidecounter = 0
    def __init__(self,name,side,Spd,cSPD,DEF,HP,cHP):
        self.name=name      
        self.side=side
        self.Spd=Spd
        self.cSPD=cSPD
        self.DEF=DEF
        self.HP=HP
        self.cHP=cHP
        char.counter =1    
        if self.side == "a":
            char.Asidecounter =1
        else:
            char.Bsidecounter =1
        activechars.append(name)

activechars=[]
defeatedchars=[]

Okay, this gives us a character and some stats. (I tried to omit some extra stats here.) I have a counter for which side they're on, a total character count and so on every time I add a character to the mix. The problem is, then I want to determine the total # of moves available to be distributed among however many characters, I try to do something like this and it just doesn't work for me:

movecount=0
for i in range(len(activechars)):
    movepick = activechars[i]
    movecount =movepick.Spd

You can see where I'm going here. I'd like to do something similar for attacks, special powers usage, whatever--put it in a loop but then call (and change) object-specific values from inside the loops, and I just don't know how to.

I've tried this with dictionaries instead of defined "char" objects; e.g.

hero={"Spd":4,"cSPD":4,"DEF":8,"HP":10,"cHP":10}

...and then rewrite the loops for a dictionary instead. But it's not working either way.

(Also, I've been told that I don't follow Python capitalization conventions, for which I apologize.)

CodePudding user response:

You're so close, it appears to be a small issue. Running your code and created char objects, I noticed you're saving the object's name attribute only into the list - for instance, "lancelot" instead of something like <__main__.char at 0x1e5770a9f48>. Simply change the last line to append self instead of name (also check indentation).

class char:                                                            
    counter = 0         #char counter to keep track of the nbr of doodz
    Asidecounter = 0                                                   
    Bsidecounter = 0                                                   
    def __init__(self,name,side,Spd,cSPD,DEF,HP,cHP):                  
        self.name=name                                                 
        self.side=side                                                 
        self.Spd=Spd                                                   
        self.cSPD=cSPD                                                 
        self.DEF=DEF                                                   
        self.HP=HP                                                     
        self.cHP=cHP                                                   
        char.counter =1                                                
        if self.side == "a":                                           
            char.Asidecounter =1                                       
        else:                                                          
            char.Bsidecounter =1                                       
        activechars.append(self)                                       
                                                                       
activechars=[]                                                         
defeatedchars=[]                 

                                  
  • Related