KontonAkumu
KontonAkumu

Reputation: 47

Python 3, list of objects? New to python

New to python and self-taught so I'm probably going about this horribly wrong but I'm trying to find the best way to list out objects while also placing them in a list. I was advised to find a way to do this by a friend to avoid double entry of creating my object then typing my object's name in. I'm open to any critiques and advice, thanks!

The way I have it set up now is giving me the error

line 16, in <module>
    Starters.append(Botamon = Digi("Botamon",0,1,1,1,1,[""]))
TypeError: list.append() takes no keyword arguments"
#Class
class Digi:
    def __init__(self,mon,age,offense,defense,speed,brains,evo):
        self.mon = mon
        self.age = age
        self.offense = offense
        self.defense = defense
        self.speed = speed
        self.brains = brains
        self.evo = evo

#Digilist
Starters = []
Starters.append(Botamon = Digi("Botamon",0,1,1,1,1,[""]))
Starters.append(Poyomon = Digi("Poyomon",0,1,1,1,1,[""]))
Starters.append(Punimon = Digi("Punimon",0,1,1,1,1,[""]))
Starters.append(Yuramon = Digi("Yuramon",0,1,1,1,1,[""]))

Digilist = []
Digilist.append(Koromon = Digi("Koromon",1,3,3,3,3,["Botamon"]))
Digilist.append(Tokomon = Digi("Tokomon",1,3,3,3,3,["Poyomon"]))
Digilist.append(Tsunomon = Digi("Tsunomon",1,3,3,3,3,["Punimon"]))
Digilist.append(Tanemon = Digi("Tanemon",1,3,3,3,3,["Yuramon"]))


#Starter
self = random.choice(Starters)
name = self.mon

Upvotes: 0

Views: 43

Answers (1)

ldall03
ldall03

Reputation: 161

The problem is you are naming your variables while appending them. If you are never going to access them by their name just do it like so:

Starters = []
Starters.append(Digi("Botamon",0,1,1,1,1,[""]))
Starters.append(Digi("Poyomon",0,1,1,1,1,[""]))
Starters.append(Digi("Punimon",0,1,1,1,1,[""]))
Starters.append(Digi("Yuramon",0,1,1,1,1,[""]))

If you have to access them by name later on, create them then append them:

Botamon = Digi("Botamon",0,1,1,1,1,[""])
Starters.append(Botamon)

and so on... However in the code example you have provided it looks like you will not have to access them by their variable name.

Upvotes: 1

Related Questions