0

Recently I have made a connect 4 game in python. Since I'm a beginner I was trying to find a way to create my counter objects when I need them instead of having to make a lot of them individually at the start of the game. I found that an exec statement works.

exec("counter%d = Counter(board,[%d,%d],'player','counter%d')" % 
(len(counters)+1,current_heights[row],row,len(counters)+1)) in globals(), locals()

I haven't had any problems with it but It seems like the kind of thing that would be called bad programming by better programmers.

  • 1
    Yes, this is a bad idea. There's no need for dynamic variables; use a dict. – Daniel Roseman Sep 28 '17 at 08:39
  • Also, use Python3 instead of Python2. And do `"counter{countNum} = Counter(board, [{one}, {two}], 'player', 'counter{countNum}')".format(countNUm=len(counters)+1, one=current_heights[row], two=row)` instead of the legacy `%` operation. – Torxed Sep 28 '17 at 08:47

0 Answers0