How do I fix TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'?

Question:

I’m trying to remake Tic-Tac-Toe on python. But, it wont work.

I tried
`

game_board = ['_'] * 9
print(game_board[0]) + " | " + (game_board[1]) + ' | ' + (game_board[2])
print(game_board[3]) + ' | ' + (game_board[4]) + ' | ' + (game_board[5])
print(game_board[6]) + ' | ' + (game_board[7]) + ' | ' + (game_board[8])

`
but it returns

`

Traceback (most recent call last):
  File "C:UsersusernamePycharmProjectspythonProjecttutorial.py", line 2, in <module>
    print(game_board[0]) + " | " + (game_board[1]) + ' | ' + (game_board[2])
    ~~~~~~~~~~~~~~~~~~~~~^~~~~~~
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

`

Asked By: username

||

Answers:

Is this you want..!?

Code:-

game_board = ['_']*9
print(game_board[0]+" | "+(game_board[1])+' | '+(game_board[2]))
print(game_board[3]+' | '+(game_board[4])+' | '+(game_board[5]))
print(game_board[6]+' | '+(game_board[7])+' | '+(game_board[8]))

Output:-

_ | _ | _
_ | _ | _
_ | _ | _
Answered By: Yash Mehta

This is because you put the parenthesis wrongly. It should be

game_board = ['_'] * 9
print(game_board[0] + " | " + (game_board[1]) + ' | ' + (game_board[2]))
print(game_board[3] + ' | ' + (game_board[4]) + ' | ' + (game_board[5]))
print(game_board[6] + ' | ' + (game_board[7]) + ' | ' + (game_board[8]))
Answered By: wavetitan

Please look at the error carefully to find your answer.

print(game_board[0]) + " | " + (game_board[1]) + ' | ' + (game_board[2])
~~~~~~~~~~~~~~~~~~~~~^~~~~~~

you have closed the bracket for game_board[0]. An additional ‘(‘ is to be used.

print( (game_board[0]) + " | " .....
Answered By: Immature trader
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.