954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Grid maker

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!

hovestar
Newbie Poster
12 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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
hovestar
Newbie Poster
12 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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
Moderator
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
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: