I am trying to make a code that allows the user to track where they have been on a grid (grok introduction to python (2) X marks the trail) I have been working on the code for a while and I think that I'm pretty close but not sure how to correctly format it
the code should work like this:
Grid size: 3
x..
...
...
Direction: right
xx.
...
...
Direction: down
xx.
.x.
...
Direction: right
xx.
.xx
...
Direction:
but mine displays:
Grid size: 3
[['x', '.', '.'], ['.', '.', '.'], ['.', '.', '.']]
Direction: down
[['x', '.', '.'], ['x', '.', '.'], ['.', '.', '.']]
Direction: right
[['x', '.', '.'], ['x', 'x', '.'], ['.', '.', '.']]
Direction: right
[['x', '.', '.'], ['x', 'x', 'x'], ['.', '.', '.']]
Direction: down
[['x', '.', '.'], ['x', 'x', 'x'], ['.', '.', 'x']]
Direction: left
[['x', '.', '.'], ['x', 'x', 'x'], ['.', 'x', 'x']]
my code is:
g = input('Grid size: ')
gn = int(g)
grid = []
for i in range(gn):
row = []
for j in range(gn):
row.append('.')
grid.append(row)
grid[0][0]='x'
print(grid)
h = 0
v = 0
m = input("Direction: ")
while m != "":
if m.lower() == "right":
h = h+1
grid[v][h] = 'x'
print(grid)
m = input("Direction: ")
elif m.lower() == "left":
h = h-1
grid[v][h]= 'x'
print(grid)
m = input("Direction: ")
elif m.lower() == "up":
v = v-1
grid[v][h]= 'x'
print(grid)
m = input("Direction: ")
elif m.lower() == "down":
v = v+1
grid[v][h]= 'x'
print(grid)
m = input("Direction: ")
Someone, PLEASE help I can't figure out how to join the lists together (each on a new line)
You could create function to print the grid
def show_grid(grid):
for line in grid:
print(''.join(line))