what I mean:
e.g.
Also note that star imports lile
import random def create_torus(max_vert, max_horz, density): torus = [] for x in range(max_vert): torus.append([]) for y in range(max_horz): torus[-1].append(int(random.randint(1, 100) < density)) return torus def pprint_torus(torus): for row in torus: print(row) MAXHORZ = 15 MAXVERT = 10 DENSITY = 50 torus = create_torus(MAXVERT, MAXHORZ, DENSITY) pprint_torus(torus)
create_torus
can be one-lier using list comprehensiondef create_torus(max_vert, max_horz, density): return [[int(random.randint(1, 100) < density) for y in range(max_horz)] for x in range(max_vert)]Now, because you say it's [variation of] Conway's Game of Life, I think it will be better to move to OOP/class structure sooner than later.
e.g.
import random class Torus: def __init__(self, max_vert, max_horz, density): self._torus = [[int(random.randint(1, 100) < density) for y in range(max_horz)] for x in range(max_vert)] @property def rows(self): return len(self._torus) @property def columns(self): return len(self._torus[0]) def __repr__(self): return f"Torus({self.rows} rows x {self.columns} columns: {', '.join(str(row) for row in self._torus)})" def __str__(self): return '\n'.join(str(row) for row in self._torus) if __name__ == '__main__': MAXHORZ = 15 MAXVERT = 10 DENSITY = 50 torus = Torus(MAXVERT, MAXHORZ, DENSITY) print(torus) print(repr(torus))Note, I am using regular lists, not
array.array
, but you get the idea. You can use whatever you want. However note that you also don't use array.array
(despite what you may think):torus = array('b') # here torus is array.array torus = [[0] * MAXHORZ] * MAXVERT # here you bind name torus to different object - list of lists, it is no longer array.array
Also note that star imports lile
from array import *
are generally considered bad practice and discouraged.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs