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 Q
s on the grid:
[['-','-','-','-','-','-','Q','-'],
['-','-','-','-','Q','Q','-','-']
['-','-','-','-','Q','-','-','-']
['Q','Q','-','-','-','Q','Q','-']
['-','-','Q','-','Q','-','-','-'].
A few things:
i
, then just insert theQ
whererandrange
tells you.randrange
call is wrong. It should have a start and a stop. In fact,rangerange
is not the right tool here if you want yourQ
s 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)