I have a function that creates a random number of instances of objects. For the sake of demonstrating the general idea, we are going to pretend that this is an algorithm for constructing a series of ridiculous rooms.
The requirements are such that I will not know how many copies will be in advance; they are generated randomly and on the fly.
As a brief note: I am fully aware that the following code is non-functional, but it should (hopefully)! demonstrate my goals.
import random
class Levelbuild(object):
def __init__(self):
self.l1 = dict({0:'a',1:'b',2:'c',3:'d',4:'e',5:'f',6:'g',7:'h',8:'i'})
for i in range(random.randint(4,9)):
self.l1[i] = Roombuilder()
Assuming the selected random integer is 5, the ideal result would be 5 instances of Roombuilder (); denoted by a, b, c, d and e, respectively.
Is there an easy way to do this? Is there any way to make this period?
- Edit -
"" . / - , , ;
import random
class Room(object):
def __init__(self):
self.size = (5,5)
class Level(object):
def __init__(self):
roomnames = ['a','b','c','d','e','f','g','h','i']
self.rooms = {}
for i in range(random.randint(4, 9)):
self.rooms[roomnames[i]] = Room()
, "" , ...
test = Level()
print test.rooms['a'].size
>>> (5,5)