Hi, I´m wondering how i can append newly created objects to a list by using subclass.I have come this far:

class Room(object):

    def __init__(self,name):
            self.name=str(name)
            self.hazard = self.empty

    #def __str__(self):
     #    return self.name
    
    def pits(self,antal):
        pit=1
        while pit<antal:
            assert self.hazard == self.empty
            self.hazard=self.Pits
 #           return self.hazard

    def bats(self,antal):
        bat=1
        while bat<antal:
            assert self.hazard == self.empty
            self.hazard=self.Bats
            #return self.hazard
            
    def wumpus(self,antal=1):
        self.wumpus= random.choice(cave_map)
class Game(Room):
    def __init__(self):
        self.map=[]
        for i in range(0,20):
            cave=Room(i+1)
            self.map.append(cave)

    def cave_map(self):
        vertical_list=self.map[:]
        horizontal_list=self.map[:]
        shuffle(vertical_list)
        for k in range len(vertical_list):
            while vertical_list[i]==horizontal_list[i]:
                shuffle(horizontal_list)

I am trying to create 20 room objects through the subclass Game and the append it to self.map but it doesn´t work, Please tell me what i am doing wrong?

You have at least much unimplemented stuff:

from random import shuffle
class Room(object):
    empty = []
    def __init__(self,name):
            self.name=str(name)
            self.hazard = self.empty
    
    def pits(self,antal):
        pit=1
        while pit < antal:
            assert self.hazard == self.empty
            self.hazard = self.Pits ## undefined

    def bats(self,antal):
        bat=1
        while bat < antal:
            assert self.hazard == self.empty
            self.hazard = self.Bats ## undefined
            
    def wumpus(self,antal=1):
        self.wumpus= random.choice(self.cave_map) ## cave_map undefined

    def __str__(self):
        return str(self.__dict__)
        
class Game(Room):
    def __init__(self):
        self.map=[]
        for i in range(0,20):
            cave=Room(i+1)
            self.map.append(cave)
        self.cave_map()

    def cave_map(self):
        vertical_list=self.map[:]
        horizontal_list=self.map[:]
        shuffle(vertical_list)
        for k in range(len(vertical_list)):
            while vertical_list[k]==horizontal_list[k]:
                shuffle(horizontal_list)
                
    def __str__(self):
        return 'Game('+', '.join(str(room) for room in self.map)+')'

    def __iter__(self):
        for value in self.map:
            yield value

mygame = Game()
for room in mygame:
    print room

Thanks for the help! Most of the unimplemented stuff is because I´m trying to create a game and i´m far from finished so it looks a bit strange at the moment, but again thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.