📜  如何在 python 代码示例中迭代地在更大的网格中创建网格

📅  最后修改于: 2022-03-11 14:45:31.296000             🧑  作者: Mango

代码示例1
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 '\n'
                           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 '\n'
                               print('+', end='')
                       elif col % 2 == 0:                  # if col item is an even number
                           print('-', end='')              # print '-' with no '\n'
                       else:                               # else if col item is uneven item num
                           print(' ', end='')              # print ' ' no '\n'
                   else:
                   # --  Formatting line structure  -- #
                       if col == 0:                        # if column is at starting position 0
                           print('|', end='')              # print '|' no '\n'
                       elif col == int(grid_cols/2):            # if column is at middle pos
                           if units % 2 == 0:               # print '|' no '\n'
                               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 '\n'