First of all you should use lists instead of tuples, tuples are immutable and cannot be changed.
Create grid as list with lists
grid = [[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]
This should do the trick, though one of the python-gurus may have an even easier solution.
First Edition
#go through all the lines
for index, line in enumerate(grid):
#is the line the first or the last one
if index == 0 or index == len(grid)-1:
#randomize all entries
for i in range(0, len(line)):
line[i] = randint(0, 1)
#the line is one in the middle
else:
#randomize the first and the last
line[0] = randint(0, 1)
line[-1] = randint(0, 1)
After toying around some more I could replace the nested for with a list comprehension to make the code more readable
Second Edition
for index, line in enumerate(grid):
if index == 0 or index == len(grid)-1:
grid[index] = [randint(0, 1)for x in line]
else:
line[0] = randint(0, 1)
line[-1] = randint(0, 1)
If someone points out an easier/more readable way to do the if I would be glad.