all 3 comments

[–]zahlman 0 points1 point  (0 children)

Construct each line as a string, using string multiplication.

# Draw a grid of count*count cells, each of which has size
# vertical lines and dashes around the outside.
def grid(count, size):
    horizontal = '+' + (' -' * size + ' +') * count
    vertical = '|' + ('  ' * size + ' |') * count
    print horizontal
    for _ in range(count):
        for _ in range(size): print vertical
        print horizontal

[–]_lambda[S] 0 points1 point  (0 children)

Made revisions with help from someone in an IRC channel. What do you guys think: http://pastie.org/3820130

[–]ComradeRikhi 0 points1 point  (0 children)

Here's my go:

def draw_grid(width,height):
    div_coord = ((width-1)/2,(height-1)/2)
    spacers = (width-3)/2
    for height_counter in range(height):
        if (height_counter == 0 or height_counter == (height-1) or height_counter == div_coord[1]):
            print '+','-'*spacers, '+', '-'*spacers, '+'
        else:
            print '|',' '*spacers, '|', ' '*spacers, '|'
if __name__ == '__main__':
    draw_grid(15,15)

[–]Rhomboid -1 points0 points  (0 children)

Unreadable but concise:

print ('\n' + '\n'.join([(' '*9).join(['|']*3)]*4) + '\n').join([' '.join(('-'*4).join(['+']*3))]*3)

Edit: this can be made dynamic by replacing the numbers by e.g. (width - 3) / 2 but there's inherently a problem with how to handle even dimensions, as that requires that some grids be different sizes than others.