Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to iteratively create a grid within a bigger grid in python

def grid_print(area, units):

   print_Area = (area * area)

   grid_rows = units + (units + 1) + 2

   grid_cols = units + 2

   if units % 2 == 0:           # If grid entry is even (it will end up making
        grid_rows += 1               # the square uneven, so increase number of rows by 1
                            # now grid is technically uneven
        for i in range(print_Area):
           for row in range(grid_rows):     # for each item in number of items(rows)
               for col in range(grid_cols): # for each item in number of items(columns)
                   if row == 0 or row == int(grid_rows/2) or row == grid_rows -1:    # if item is beginning, middle or end
                   # --  Formatting beam structure  -- #
                       if col == 0:                        # beginning, print '+' no '
'
                           print('+', end='')
                       elif col == grid_cols -1:                # end, print '+'
                       print('+')
                       elif int(grid_cols/2) == col:            # middle:
                           if grid_rows % 2 == 0:               # if grid is even, pad '+' with ' '
                               print(' + ', end='')        # if grid is uneven, no padding
                           else:                           # print '+' no '
'
                               print('+', end='')
                       elif col % 2 == 0:                  # if col item is an even number
                           print('-', end='')              # print '-' with no '
'
                       else:                               # else if col item is uneven item num
                           print(' ', end='')              # print ' ' no '
'
                   else:
                   # --  Formatting line structure  -- #
                       if col == 0:                        # if column is at starting position 0
                           print('|', end='')              # print '|' no '
'
                       elif col == int(grid_cols/2):            # if column is at middle pos
                           if units % 2 == 0:               # print '|' no '
'
                               print(' | ', end='')        # (has padding if grid is even or not)
                           else:
                               print('|', end='')
                       elif col == grid_cols -1:                # if column is at end position of grid
                           print("|")                      # print '|'
                       else:
                           print(' ', end='')              # all other circumstances, print ' ' no '
'
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #iteratively #create #grid #bigger #grid #python
ADD COMMENT
Topic
Name
1+1 =