1

I am trying to resolve the error as highlighted in the picture below. I am able to print a tic-tac-toe board but the trailing whitespace affects the test case. I am not better yet at using strings to represent a list of numbers. The desired output is embedded in the image below: test case error

def printBoard(plane):#function to print out the current board
    for w in range(len(plane)):
        for e in range(len(plane)):
            if plane[w][e]== 1:
                print('{:^3}'.format('X'), end="")
            elif plane[w][e] == -1:
                print('{:^3}'.format('O'), end="")
            elif plane[w][e] == 0:
                print('{:^3d}'.format((len(plane)*w+e)), end="")
        print()
  • Can you provide an example usage of this function? – Michael M. Nov 01 '22 at 03:15
  • @MichaelM. This function initializes the tictactoe board from 0 - 36 before the players start appending "X" or "O" – Frankline Misango Nov 01 '22 at 03:18
  • It seems like this function you provided prints a board, not initializes one. – Michael M. Nov 01 '22 at 03:23
  • @MichaelM. yes..it prints the initial empty (with pure numbers only) board before we start "X" and "O" in the next player turns...sorry for the wording – Frankline Misango Nov 01 '22 at 03:31
  • Does this answer your question? [Format output string, right alignment](https://stackoverflow.com/questions/8234445/format-output-string-right-alignment) – OrenIshShalom Nov 01 '22 at 04:44
  • side note: try to make future questions as *concise* and *short* as possible. Remove `'X'` and `'O'` related statements if they are not used. paste console / terminal output rather than inserting images – OrenIshShalom Nov 01 '22 at 04:47

2 Answers2

1

I believe you are looking to right justify the numbers while also enforcing 3 characters of width so your printed board retains its formatting. To do this, you can eliminate the caret character from your print statements, which will allow the printed values to right justify. The caret signifies that you are centering the text, which will cause it to leave trailing character(s). This should eliminate your trailing whitespace:

print('{:3d}'.format((len(plane)*w+e)), end="")
Chris Lindseth
  • 711
  • 1
  • 2
  • 12
0

This will do want you want. Changed end to vary if it was the end of a line and used periods(.) to make the spaces visible for demonstration. Refactored a bit to remove redundant code:

def printBoard(plane):
    i=0
    for values in plane:
        for col,value in enumerate(values):
            text = ' X' if value==1 else ' O' if value==-1 else f'{i:2d}'
            print(text, end='\n' if col==len(values)-1 else '.')
            i += 1

plane = [[0]*6 for _ in range(6)]
printBoard(plane)
plane[1][1] = 1
plane[1][5] = 1
plane[2][2] = -1
plane[2][5] = -1
print()
printBoard(plane)

Output:

 0. 1. 2. 3. 4. 5
 6. 7. 8. 9.10.11
12.13.14.15.16.17
18.19.20.21.22.23
24.25.26.27.28.29
30.31.32.33.34.35

 0. 1. 2. 3. 4. 5
 6. X. 8. 9.10. X
12.13. O.15.16. O
18.19.20.21.22.23
24.25.26.27.28.29
30.31.32.33.34.35
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251