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)
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
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.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
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)
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
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])
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852