如何在python中向网格添加点

如何在python中向网格添加点,python,css,grid,Python,Css,Grid,所以我有一个创建网格的代码(有点长) 它将打印一个网格,您可以在其中决定其宽度和高度。以及可以向栅格添加点的位置。例如,栅格(宽度=15,高度=5)提供的栅格如下所示: Enter the width: 15 Enter the height: 5 123456789012345 +--------------- 1|............... 2|............... 3|............... 4|............... 5|...............

所以我有一个创建网格的代码(有点长)

它将打印一个网格,您可以在其中决定其宽度和高度。以及可以向栅格添加点的位置。例如,栅格(宽度=15,高度=5)提供的栅格如下所示:

Enter the width: 15
Enter the height: 5
  123456789012345
 +---------------
1|...............
2|...............
3|...............
4|...............
5|...............
之后,您可以将X点添加到您创建的网格中,如图所示

Shall we add a point (Y/N)? y
Enter X-coordinate: 13
Enter Y-coordinate: 2
  123456789012345
 +---------------
1|...............
2|............X..
3|...............
4|...............
5|...............
然后我的问题来了。我不知道如何保存这个X点,使其保留在网格上。之后,我应该可以向网格添加新的X点。因此,如果我想在(2,2)中添加新的X点,它看起来像

Shall we add a point (Y/N)? y
Enter X-coordinate: 2
Enter Y-coordinate: 2
  123456789012345
 +---------------
1|...............
2|.X..........X..
3|...............
4|...............
5|...............
所以基本上我的问题是我不知道如何保存对网格的更改。
提前感谢。

您需要保留以前X坐标的列表

解释 Python有一个数据类型

您可以将项目添加到列表中,也可以检查项目是否已在列表中

下面演示了列表的工作原理:

points = []
print(points)
if [1,2] in points:
    print("Found you")
else:
    print("It isn't here")

points.append([1,2])
points.append([5,6])
points.append([7,3])
print(points)
if [1,2] in points:
    print("Found you")
else:
    print("It isn't here")
它给出以下输出:

[]
它不在这里
[[1, 2], [5, 6], [7, 3]]
找到你了
修正你的代码 我已经修改了您的代码并发表了评论,以向您展示如何:

width = int(input('Enter the width: '))
height = int(input('Enter the height: '))

# print the grid

# prints y-axel
y = 1
print('  ', end='')
for x in range(0, width):
    print(y, end='')
    y = y + 1
    if y == 10:
        y = 0
print('')

print(' +', '-' * (width), sep='')  # prints the top row
# prints all remaining x-axels
z = 1
for i in range(0, height):
    print(z, '|', '.' * width, sep='')
    z = z + 1
    if z == 10:
        z = 0

# this is a list of all the xpoints
xpoints = []

# Keep asking until the user says no
while True:
    # ask do you wanna put a point in a grid
    point = input('Shall we add a point (Y/N)? ')
    if point == 'Y' or point == 'y':
        x_coord = int(input('Enter X-coordinate: '))
        y_coord = int(input('Enter Y-coordinate: '))
        xpoints.append([x_coord, y_coord])  # add the new point to the list

        # prints y-axel
        y = 1
        print('  ', end='')
        for x in range(0, width):
            print(y, end='')
            y = y + 1
            if y == 10:
                y = 0
        print('')

        # prints all remaining x-axels
        print(' +', '-' * (width), sep='')
        z = 1
        # for every row
        for i in range(0, height):
            line = str(z)+"|" # This string will hold the line as we add dots to it
            # for every point in the row
            for x in range(width):
                # if this point is an X point:
                if [x, i] in xpoints:
                    line += 'X'
                # if it isn't:
                else:
                    line += '.'
            print(line) # Show the line
            z = z + 1
            if z == 10:
                z = 0
    else:
        break

您需要保留一份以前X坐标的列表

解释 Python有一个数据类型

您可以将项目添加到列表中,也可以检查项目是否已在列表中

下面演示了列表的工作原理:

points = []
print(points)
if [1,2] in points:
    print("Found you")
else:
    print("It isn't here")

points.append([1,2])
points.append([5,6])
points.append([7,3])
print(points)
if [1,2] in points:
    print("Found you")
else:
    print("It isn't here")
它给出以下输出:

[]
它不在这里
[[1, 2], [5, 6], [7, 3]]
找到你了
修正你的代码 我已经修改了您的代码并发表了评论,以向您展示如何:

width = int(input('Enter the width: '))
height = int(input('Enter the height: '))

# print the grid

# prints y-axel
y = 1
print('  ', end='')
for x in range(0, width):
    print(y, end='')
    y = y + 1
    if y == 10:
        y = 0
print('')

print(' +', '-' * (width), sep='')  # prints the top row
# prints all remaining x-axels
z = 1
for i in range(0, height):
    print(z, '|', '.' * width, sep='')
    z = z + 1
    if z == 10:
        z = 0

# this is a list of all the xpoints
xpoints = []

# Keep asking until the user says no
while True:
    # ask do you wanna put a point in a grid
    point = input('Shall we add a point (Y/N)? ')
    if point == 'Y' or point == 'y':
        x_coord = int(input('Enter X-coordinate: '))
        y_coord = int(input('Enter Y-coordinate: '))
        xpoints.append([x_coord, y_coord])  # add the new point to the list

        # prints y-axel
        y = 1
        print('  ', end='')
        for x in range(0, width):
            print(y, end='')
            y = y + 1
            if y == 10:
                y = 0
        print('')

        # prints all remaining x-axels
        print(' +', '-' * (width), sep='')
        z = 1
        # for every row
        for i in range(0, height):
            line = str(z)+"|" # This string will hold the line as we add dots to it
            # for every point in the row
            for x in range(width):
                # if this point is an X point:
                if [x, i] in xpoints:
                    line += 'X'
                # if it isn't:
                else:
                    line += '.'
            print(line) # Show the line
            z = z + 1
            if z == 10:
                z = 0
    else:
        break

您应该使用列表列表作为网格,并在用户输入内容时将索引[y][x]处的相应元素更改为“x”

grid = [['-' for i in range(9)] for j in range(7)]

y = 5
x = 3
# Note that y comes first.
grid[y][x] = 'X'

for row in grid:
    for item in row:
        print(item, end='')
    print()

您应该使用列表列表作为网格,并在用户输入内容时将索引[y][x]处的相应元素更改为“x”

grid = [['-' for i in range(9)] for j in range(7)]

y = 5
x = 3
# Note that y comes first.
grid[y][x] = 'X'

for row in grid:
    for item in row:
        print(item, end='')
    print()

栅格是二维阵列:

width = int(input('Enter the width: '))
height = int(input('Enter the height: '))

grid = []
for i in range(height):
    grid.append(['.']*width)

def print_grid(grid):
    # Header: '  123456789012345'
    print('  ', end='')
    for i in range(width):
        print(i%10, end='')
    print('')  # New line
    # Table: ' +---------------'
    print(' +', end='')
    for i in range(width):
        print('-', end='')
    print('')  # New line
    # Rows
    for i, row in enumerate(grid):
        print(str(i) + '|', end='')
        for cell in row:
            print(cell, end='')
        print('')  # New line

def fill_grid(grid, x, y):
    grid[y-1][x-1] = 'X'
用法:

fill_grid(grid, 13, 3)
print_grid(grid)

栅格是二维阵列:

width = int(input('Enter the width: '))
height = int(input('Enter the height: '))

grid = []
for i in range(height):
    grid.append(['.']*width)

def print_grid(grid):
    # Header: '  123456789012345'
    print('  ', end='')
    for i in range(width):
        print(i%10, end='')
    print('')  # New line
    # Table: ' +---------------'
    print(' +', end='')
    for i in range(width):
        print('-', end='')
    print('')  # New line
    # Rows
    for i, row in enumerate(grid):
        print(str(i) + '|', end='')
        for cell in row:
            print(cell, end='')
        print('')  # New line

def fill_grid(grid, x, y):
    grid[y-1][x-1] = 'X'
用法:

fill_grid(grid, 13, 3)
print_grid(grid)

可以在列表中存储栅格点坐标。你的代码适应性不强。例如,您有两个单独的代码块打印出网格。一个打印空网格,另一个打印单点网格。不管有多少个点,都需要有一块代码来打印网格。这可能意味着在循环中每次打印一个栅格坐标,每次检查当前坐标对是否在列表中,如果在列表中,则打印一个“X”。您可以在列表中存储栅格点坐标。你的代码适应性不强。例如,您有两个单独的代码块打印出网格。一个打印空网格,另一个打印单点网格。不管有多少个点,都需要有一块代码来打印网格。这可能意味着在循环中每次打印一个网格坐标,每次检查当前坐标对是否在列表中,如果在列表中,则打印一个“X”。您的代码是正确的,只是X点始终位于(X+1,y+1)。所以它在两个轴上都超出了1。因此,我只需将xpoints中的
if[x,i]修正为
如果xpoints中的[x+1,i+1],
取决于你是
0
还是
1
索引数组。但是是的,你的UI是
1
索引的,我应该看到:)你的代码是正确的,除了X点总是指向(X+1,y+1)。所以它在两个轴上都超出了1。因此,我只需将xpoints中的
if[x,i]修正为
如果xpoints中的[x+1,i+1],
取决于你是
0
还是
1
索引数组。但是是的,你的UI是
1
索引的,我应该看到:)