Total beginner Tic Tac Toe game in Python I've decided to learn Python about 2 weeks ago, been going through various books and videos, and I've decided to try my hand at programming a Tic Tac Toe game. I was somewhat successful (it doesn't recognize if there's already a mark in a certain spot and allows overwriting of already placed marks) and I was wondering if any more experienced programmers could give me some general feedback about how I could do things better, and how to prevent overwriting of previously placed mark.


board = ['-'] * 9

def print_board():
    print (board[0] + '|' + board[1] + '|' + board[2])
    print (board[3] + '|' + board[4] + '|' + board[5])
    print (board[6] + '|' + board[7] + '|' + board[8])




legalMoves = [1,2,3,4,5,6,7,8,9]
print_board()

turnCount = 0
def move():



    move = int(input('Pick a number 1-9:'))
    while move not in legalMoves:
        print('Illegal move')
        move = int(input('Pick a number 1-9:'))



    marks = ['X','O']


    if turnCount % 2 == 0:
        board[move - 1] = marks[1]
    else:
        board[move - 1] = marks[0]









while True:
    if board[0] == board[1] == board[2] == 'X'\
    or board[3] == board[4] == board[5] == 'X'\
    or board[6] == board[7] == board[8] == 'X'\
    or board[0] == board[3] == board[6] == 'X'\
    or board[1] == board[4] == board[7] == 'X'\
    or board[2] == board[5] == board[8] == 'X'\
    or board[0] == board[4] == board[8] == 'X'\
    or board[2] == board[4] == board[6] == 'X' \
    or board[0] == board[1] == board[2] == 'O' \
    or board[3] == board[4] == board[5] == 'O' \
    or board[6] == board[7] == board[8] == 'O' \
    or board[0] == board[3] == board[6] == 'O' \
    or board[1] == board[4] == board[7] == 'O' \
    or board[2] == board[5] == board[8] == 'O' \
    or board[0] == board[4] == board[8] == 'O':
        print('Victory')
        break

    else:
        move()
        print_board()
        turnCount = turnCount + 1
Here are my notes:

your printBoard function has code repetition, you can see there is a pattern 0 1 2, 3 4 5, 6 7 8, so  you can do a for loop, example:

for i in range(3):
  print (board[i * 3] + '|' + board[i * 3 + 1] + '|' + board[i * 3 + 2])


your legalMoves list is not required, because it's just numbers from 1 to 9, so you can check against that example: if n &gt;= 1 and n &lt;= 9: #accepted

"move = int(input('Pick a number 1-9:'))", you need to check for the input first if it can be converted to int or not to prevent the user from breaking your program if non int string value was typed, and to do that you can store the input in a string then check against it to be an int or not example:

def isInt(strInt):
  for c in strInt:
    if c not in "0123456789": return False
  return True

n = input()
if isInt(n): move = int(n)


and your checks inside the while loop can be shortened to using for loop instead, you can see a pattern in those cells positions, so if you want to check horizontally, yoou need to do, example:

for i in range(3):
  if board[i * 3] == board[i * 3 + 1] == board[i * 3 + 2] and board[i * 3] != "-":
    # do something


and vertically

for i in range(3):
  if board[i] == board[i + 3] == board[i + 6] and board[i] != "-":
    # do something


and diagonally

  if (board[0] == board[4] == board[8] or board[2] == board[4] == board[6]) and board[4] != "-":


without checking for "X" or "O", you just need to check for "-"

you need also to prevent the user from overwriting an already filled cell, example

if board[move - 1] != "-":
  print("Pls choose an empty cell")


you need also to check for tie, because not all time you get a winner example if your main loop I mean the one for each user correct move which are 9 moves, if it reaches the end and there is no winner then it's a tie, and you should let the user know.

That's it, you can improve your code now, I hope you like my feedBack, also I have written a full solution, you can find it in your question in Stack Overflow, and if you have any question, I will be happy to answer.