J.Amateur
J.Amateur

Reputation: 13

How to add points to grid in python

So i have a code that creates a grid (its a bit long)

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

# 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: '))

    # 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 remaing x-axels
    print(' +', '-' * (width), sep='')
    z = 1
    for i in range(0, height):
        if i != (y_coord - 1):
            print(z, '|', '.' * (width), sep='')

        if i == (y_coord - 1):
            print(z, '|', '.' * (x_coord - 1), 'X', '.' * (width - x_coord),
                  sep='')  # add the point
        z = z + 1
        if z == 10:
            z = 0

Which will print a grid where you can decide its width and height. And where you can add points to a grid. For example grid (width=15 and height=5) gives you a grid that looks like:

Enter the width: 15
Enter the height: 5
  123456789012345
 +---------------
1|...............
2|...............
3|...............
4|...............
5|...............

after that you can add X-points to the grid that you made like

Shall we add a point (Y/N)? y
Enter X-coordinate: 13
Enter Y-coordinate: 2
  123456789012345
 +---------------
1|...............
2|............X..
3|...............
4|...............
5|...............

after that comes my problem. I dont know how to save this X-point so it would remain on the grid. After that i should be available to add new X-points to the grid. So if i wanted to add new X-point to (2,2) it would look like

Shall we add a point (Y/N)? y
Enter X-coordinate: 2
Enter Y-coordinate: 2
  123456789012345
 +---------------
1|...............
2|.X..........X..
3|...............
4|...............
5|...............

so basically my problem is that i dont know how to save changes to my grid. Thanks in advance.

Upvotes: 1

Views: 2263

Answers (3)

Morgoth
Morgoth

Reputation: 5176

You need to keep a list of previous X co-ordinates.

Explanation

Python has a list data type.

You can add items to a list, ad you can check to see if items are in the list already.

The following demonstrates how lists work:

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")

It gives this output:

[]
It isn't here
[[1, 2], [5, 6], [7, 3]]
Found you

Fix your code

I have amended your code and commented to show you how:

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

Upvotes: 0

skrx
skrx

Reputation: 20438

You should use a list of lists as your grid and change the according element at index [y][x] to 'X' when the user enters something.

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()

Upvotes: 0

Adirio
Adirio

Reputation: 5286

Your grid is a 2D array:

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'

Usage:

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

Upvotes: 0

Related Questions