1

I'm trying to print strings in a matrix. But I couldn't find a solution for that.

game_size = 3
matrix = list(range(game_size ** 2))
def board():
    for i in range(game_size):
        for j in range(game_size):
            print('%3d' % matrix[i * game_size + j], end=" ")
        print()
board()
position = int(input("Where to replace ?"))
matrix[position] = "X"
board()

First it prints this as exactly what I want

  0   1   2 
  3   4   5 
  6   7   8
Where to replace ?5

Then It came up with an error;

TypeError: %d format: a number is required, not str

How can I solve this problem. I want my output like;

  0   1   2 
  3   4   X 
  6   7   8 

Also X should be stored in array, just printing that doesn't work Output should be same format as it is.

4
  • use %s instead of %d Commented Nov 18, 2019 at 12:11
  • Problem is here: matrix[position] = "X". From now on, the inserted object is a str type containing "X". However, in the loop you want to reformat it to %3d which is obviously illogical. You cannot reformat the "X" string to a number unless it is a number stored as a string. Commented Nov 18, 2019 at 12:19
  • Do you think is there a solution for that. I thinked maybe it would solve with if statements. But I couldn't make it. Commented Nov 18, 2019 at 12:21
  • Yes, sure @Ahmeed_Hawary already answered you. Use the %s (string) format istead of %d (number). So you must to be working with a string indentation as %3s and not with a number indentation such as %3d. Or better is to use the new method: str.format(). So there will the following source code: print('{0:>3}'.format(.....value....),end='') Commented Nov 18, 2019 at 12:40

1 Answer 1

1

You are currently using a format string which requires that all the inputs are integers. I've changed this to using f-strings in the solution below.

game_size = 3
matrix = list(range(game_size ** 2))
def board():
    for i in range(game_size):
        for j in range(game_size):
            print(f'{matrix[i * game_size + j]}'.rjust(3), end=" ")
        print()
board()
position = int(input("Where to replace ?"))
matrix[position] = "X"
board()

Output with game_size=3:

0   1   2   
3   4   5   
6   7   8   

Where to replace ?5
0   1   2   
3   4   X   
6   7   8  

Output with game_size=5:

  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 

Where to replace ?4
  0   1   2   3   X 
  5   6   7   8   9 
 10  11  12  13  14 
 15  16  17  18  19 
 20  21  22  23  24 
Sign up to request clarification or add additional context in comments.

4 Comments

this doesn't work for me because i want it in that format there should be gaps between numbers
Actually, this is not dynamic. If i give gamesize 5, last 3 row skids :(
Yes, it will work, but if you run the code on an older Python version where the print(f"{foobar}") formatting style did not exist, the code will not work there. Use standard formatting rather...as I mentioned above in my comments. So use print("{}".format(foobar)) instead of the new style format.
BTW this method .rjust() is not a neccessary ! You can use the following syntax instead: print(f'{matrix[ i * game_size + j ]:>3}', end='')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.