-1
class Card:
  def __init__(self, row, col):
    self.row = row
    self.col = col

for c in range(1,7):
    for r in range(1,7):
      Card_"row"-"col" = Card(r,c) 

I want each card to be named based on its row and column. Ex. "Card_1-2" would be in row 1 and column 2, so that I can reference each card easier. Is there a way I can do that or is there a better way to look at the problem? Either way can someone please help me.

  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables). What you are looking for is a dictionary. Naming variables will not make it easier, but accessing a dictionary will – Tomerikoo Dec 17 '19 at 19:12
  • `Card_1-2` isn't a valid variable name, even if this was a good idea. – chepner Dec 17 '19 at 19:15

2 Answers2

2

Dictionary using cell coordinates is an easy solution in your case:

grid = {}
for r in range(1,7):
  for c in range(1,7):
    grid[r,c] = Card(r,c) 

So each card may be later accessed by grid[r,c]

sciroccorics
  • 2,357
  • 1
  • 8
  • 21
0

Create a dictionary that maps your desired name to each card.

from itertools import product
cards = {f"Card_{row}-{col}": Card(row, col) 
         for row, col in product(range(1,7), repeat=2)}
chepner
  • 497,756
  • 71
  • 530
  • 681