Hello I'm working on printing a grid of any size and labeling it numerically or alphabetically I have the code that creates the grid, but I have no idea how I would label it... Here is the code that crates the grid:

def grid():
	y = 5 #int(raw_input("How tall do you want the grid to be?" "(type a to exit)"))
	x = 5 #int(raw_input("How long do you want the grid to be?"))
	a = ''' _____z
|    |w
|____|xy'''
	for i in range(y-1):
		a = a.replace('''y''','''
|    |w
|____|xy''')
	for j in range(x-1):
		a = a.replace('''x''','''____|x''')
		a = a.replace('''w''','''    |w''')
		a = a.replace('''z''','''_____z''')
	a = a.replace('''x''','''''')
	a = a.replace('''y''','''''')
	a = a.replace('''_z''','''''')
	a = a.replace('''w''','''''')
	print a

grid()

Any help is appreciated. Thanks!

Oops I forgot to change something. Here is the real code:

def grid():
	y = int(raw_input("How tall do you want the grid to be?" "(type a to exit)"))
	x = int(raw_input("How long do you want the grid to be?"))
	a = ''' _____z
|    |w
|____|xy'''
	for i in range(y-1):
		a = a.replace('''y''','''
|    |w
|____|xy''')
	for j in range(x-1):
		a = a.replace('''x''','''____|x''')
		a = a.replace('''w''','''    |w''')
		a = a.replace('''z''','''_____z''')
	a = a.replace('''x''','''''')
	a = a.replace('''y''','''''')
	a = a.replace('''_z''','''''')
	a = a.replace('''w''','''''')
	print a

Your style is bit esoteric, especially using multiline single quotes everywhere, here is maybe little more conventional way:

def grid(x, y):
    first = (3* ' ' + ''.join('%c'.center(7)  % (ord('A') + n) for n in range(x)) + '\n   ' +
             (6 * '_') * x + '\n')
    rest = (3 * ' ' + x * '|     ' + '|\n' +
            '%3s' + x * '|     ' + '|\n')
    rest += 3 * ' ' + x * '|_____' + '|\n'
    return first + (y * rest) % tuple(range(1, y + 1))

print grid(3,9)

but I have no idea how I would label it

The labels would be outside of "grid" if you want to use it in a game, and so change the contents. They would be incorporated in a print_grid() function as the only time you require labels is when the grid is printed.. It is possible, but places unnecessary complexity to include the labels or any formatting characters in the container as you would no longer have a simple row and column assignment, but would have to adjust for the labels.

Edited 4 Years Ago by woooee: n/a

A simple example:

def print_grid(a_grid):
    print "The Grid"
    for row in a_grid:
        print " |".join(row)
        print "--+--+--"
    print " X axis"
    print "\n"

y = int(raw_input("How tall do you want the grid to be?" "(type a to exit) "))
x = int(raw_input("How long do you want the grid to be? "))
a_grid = []
for a_row in range(x):
    row = [str(a_row+col) for col in range(y)] # fill with anything unique for testing
    a_grid.append(row)
print a_grid         ## just the container
print_grid(a_grid)

## change a cell
a_grid[1][2] = "Xx"     ## assumes this grid is at least 2X3
print_grid(a_grid)

Edited 4 Years Ago by woooee: n/a

Your style is bit esoteric, especially using multiline single quotes everywhere, here is maybe little more conventional way:

def grid(x, y):
    first = (3* ' ' + ''.join('%c'.center(7)  % (ord('A') + n) for n in range(x)) + '\n   ' +
             (6 * '_') * x + '\n')
    rest = (3 * ' ' + x * '|     ' + '|\n' +
            '%3s' + x * '|     ' + '|\n')
    rest += 3 * ' ' + x * '|_____' + '|\n'
    return first + (y * rest) % tuple(range(1, y + 1))

print grid(3,9)

Putting data in by format string place markers:

import string

def grid(x, y):
    first = (3* ' ' + ''.join('%c'.center(7)  % (ord('A') + n) for n in range(x)) + '\n   ' +
             (6 * '_') * x + '\n')
    rest = (3 * ' ' + x * '|     ' + '|\n' +
            '%3s' + x * '|%%3s  ' + '|\n')
    rest += 3 * ' ' + x * '|_____' + '|\n'
    return first + (y * rest) % tuple(range(1, y + 1))

print grid(3, 4) % tuple(string.ascii_lowercase[:3 * 4])
This question has already been answered. Start a new discussion instead.