Python: X 标记路径
Python: X marks the trail
我正在尝试制作一个代码,允许用户跟踪他们在网格上的位置(grok 介绍 python (2) X 标记路径)我一直在为以下代码工作有一段时间,我认为我非常接近但不确定如何正确格式化它
代码应该像这样工作:
Grid size: 3
x..
...
...
Direction: right
xx.
...
...
Direction: down
xx.
.x.
...
Direction: right
xx.
.xx
...
Direction:
但我的显示:
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']]
我的代码是:
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: ")
有人,请帮助我不知道如何将列表连接在一起(每个都在一个新行上)
您可以创建函数来打印网格
def show_grid(grid):
for line in grid:
print(''.join(line))
我正在尝试制作一个代码,允许用户跟踪他们在网格上的位置(grok 介绍 python (2) X 标记路径)我一直在为以下代码工作有一段时间,我认为我非常接近但不确定如何正确格式化它
代码应该像这样工作:
Grid size: 3
x..
...
...
Direction: right
xx.
...
...
Direction: down
xx.
.x.
...
Direction: right
xx.
.xx
...
Direction:
但我的显示:
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']]
我的代码是:
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: ")
有人,请帮助我不知道如何将列表连接在一起(每个都在一个新行上)
您可以创建函数来打印网格
def show_grid(grid):
for line in grid:
print(''.join(line))