I have a list of lists of - characters which acts as a grid.
I want to change one - to a Q per col and row.
Here's what I've got so far:
import pprint
import random # I import these classes
grid = [['-'] for n in range(8)]
for i in range (8):
for j in range(8):
inserPoint = random.randrange(8,8)
if (j == inserPoint or i == inserPoint) and (grid[j] != 'Q' or grid[i] != 'Q'):
grid[i][j] = ('Q')
pprint.pprint(grid) #/ how to print one queen per line
this is my output. As you can see there are too many Qs on the grid:
[['-','-','-','-','-','-','Q','-'],
['-','-','-','-','Q','Q','-','-']
['-','-','-','-','Q','-','-','-']
['Q','Q','-','-','-','Q','Q','-']
['-','-','Q','-','Q','-','-','-'].
A few things:
i, then just insert theQwhererandrangetells you.randrangecall is wrong. It should have a start and a stop. In fact,rangerangeis not the right tool here if you want yourQs to be unique in row and column.Here's all three things fixed:
Which gives:
(See this for why shuffling a range doesn't work in Python 3)