0

I am trying to print a Tic-Tac-Toe board in the terminal, and give the user an option to choose how big the board should be. This is my code:

sq = [["       ", "|"],
      ["   ", "1", "   ", "|",],
      ["       ", "|"],
      ["-------","|"]]

def square(board):
    for row in board:
        for slot in row:
            print(slot, end="")
        print()

number = int(input("How many squares do you want to board to be?\n"))

for x in range(number):
    square(sq)

The problem is that they don't print on one line. I want to print the rows and then columns. I tried adding end="" in the "for loop" but that didn't help.

Could somebody please explain to me what i need to change in my code in order to be able to print to the same row?

BeeFriedman
  • 1,800
  • 1
  • 11
  • 29
  • Does this answer your question? [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – MAK Dec 25 '20 at 06:06
  • Thanks @MAK, but this doesn't help me, end="" also removes the newline, and it didn't help when i tried it. – BeeFriedman Dec 25 '20 at 06:15

2 Answers2

2

Probably not the best way of doing this but

sq = [["       ", "|"],
      ["   ", "1", "   ", "|",],
      ["       ", "|"],
      ["-------","|"]]

def square(board, number):
    for row in board:
        for _ in range(number):
            for slot in row:
                print(slot, end="")
        print()


number = 5
for x in range(number):
    square(sq, number)

Will give you what you want.

You need to print all the required lines in the first line and then move on to the next line using print(). Once you go to the next line, you can't go back.

Ananda
  • 2,925
  • 5
  • 22
  • 45
1

You can remove the slot loop and bring the x loop inside the row loop like this:

def square(board):
    for row in board:
        for x in range(number):
            print(''.join(row),end='')
        print()

Just add another x loop over the whole thing and you will have your tic tac toe board:

def square(board):
    for x in range(number):
        for row in board:
            for x in range(number):
                print(''.join(row),end='')
            print()
Vedant36
  • 318
  • 1
  • 6