How to make the two different code outputs appear in the same area?

Question:

I am trying to merge two outputs together so that it appears something like this:

  0 1 2
0 ? ? ?
1 ? ? ?
2 ? ? ?

But it ended up appearing like this instead:

0 1 2
0
1
                              ? ? ?
                              ? ? ?

I tried this to make the codes appear but i have no idea how to place their outputs together

import random

rows = [3]
columns = [4]

def rowscol():
    for j in range(columns[0]):
        print(" " * 1, end="")
        print(j, end="")
    print()
    for i in range(rows[0]):
        print(i)
rowscol()

def create_game_board(rows, columns):
    board = [[random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ") for _ in range(columns[0])] for _ in range(rows[0])]
    # If number of cells is odd, make the last cell an unused cell
    if (rows[0] * columns[0]) % 2 != 0:
        board[-1][-1] = "@"
    return board

board = create_game_board(rows,columns)

# Function to display the game board
def display_board(board):
    pad = " " * 30
    for row in board:
        line = pad + " ".join('?' if column != '@' else '@' for column in row)
        print(line)
            
display_board(board)
Asked By: user19600611

||

Answers:

Maybe something like?

def draw_board(board):
    print("  " + " ".join([str(i) for i in range(len(board[0]))])) # print column numbers
    for i in range(len(board)):
        row = ""
        for j in range(len(board[i])):
            row += board[i][j] + " "
        print(str(i) + " " + row)

draw_board(board)
Answered By: CodingTil

Welcome to StackOverflow!

When using multidimensional arrays, like in your case a list of lists, I like to be able to index into them easily. Because of this I usually change it to a dict and use the coordiantes as keys. This way you can even store additional information about the board, like the dimension sizes or anything else.
I added a bunch of comments to explain how the code works but feel free to ask if anything isn’t clear:

import random
import string


def create_game_board(rows, cols):
    board = dict()
    # save dimensions inside the dict itself
    board['cols'] = cols
    board['rows'] = rows
    for y in range(rows):
        for x in range(cols):
            # add random letter to board at (x,y)
            # x,y makes a tuple which can be a key in a dict
            # changed to use string.ascii_uppercase so that you don't forget any letter
            board[x, y] = random.choice(string.ascii_uppercase)

    # change last element to @ when both dimensions are odd
    if (rows * cols) % 2 == 1:
        board[rows-1, cols-1] = "@"
    return board


def display_board(board):
    # get dimensions
    cols, rows = board['cols'], board['rows']
    # print header
    print(' '.join([' '] + [str(x) for x in range(cols)]))
    for y in range(rows):
        # print rows
        #print(' '.join([str(y)] + [board[x, y] for x in range(cols)]))  # to display the actual letter at this location
        print(' '.join([str(y)] + ['?' if board[x, y] == '@' else '@' for x in range(cols)])) # using your display function
    print()  # separator empty line


board = create_game_board(3, 3)
display_board(board)

The output is nothing special when I’m using your method of printing, you might need to change that, I’m not sure how you wanted to display it. I added a line that allows you to print the values on those coordinates.
This is the output:

  0 1 2
0 @ @ @
1 @ @ @
2 @ @ ?
Answered By: Gábor Fekete
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.