So I am very new to programming and I am trying to pick up Python as a first language. I have programmed a simple game of naughts and crosses, but it is very wordy and there must be a simpler/quicker way of doing this! Any modifications or tips would be gratefully received. from random import randint#initialise gameboard = []for i in range(3):    board.append([*]*3)def print_board(board):    for row in board:        print  .join(row)print Let's play naughts and crosses! I'll move first.\nprint Here's the board:\nlist = [[' ','0','1','2'],['0','*','*','*'],['1','*','*','*'],['2','*','*','*']]def print_list(list):    for row in list:        print  .join(row)print_list(list)print  def rand_row(board):    return randint(0, len(board)-1)def rand_col(board):    return randint(0,len(board[0])-1)#Computer turn initialprint And here's my first move:\ncomp_row = rand_row(board)comp_col = rand_col(board)board[comp_row][comp_col] = Oprint_board(board)print for turn in range(20):    #Human turn    print Your turn!\n    play_row = int(input(Choose your row: ))    play_col = int(input(Choose your column: ))    if (play_row < 0 or play_row > 2) or (play_col < 0 or play_col > 2):        print That's not even on the board!        print    elif board[play_row][play_col] == O:        print Hey! That was my move!\n    else:        board[play_row][play_col] = X        print_board(board)        print     #Winning condition        if board[0] == [X,X,X] or board[1] == [X,X,X] or board[2] == [X,X,X]:        print Congratulations, you win!        break    if (board[0][0] == X and board[1][0] == X and board[2][0] ==X) or (board[0][1] == X and board[1][1] == X and board[2][1] ==X) or (board[0][2] == X and board[1][2] == X and board[2][2] ==X):        print Congratulations, you win!        break    if (board[0][0] == X and board[1][1] == X and board[2][2] == X) or(board[0][2] == X and board[1][1] == X and board[2][0] == X):        print Congratulations, you win!        break    #Computer turn    printNow it's my turn again\n    comp_row = rand_row(board)    comp_col = rand_col(board)    #Conditions for override of random generator (rows)     if board[0][0] == O and board[0][1]==O and board[0][2] == *:        comp_row = 0        comp_col = 2    elif board[0][0] == O and board[0][1]==* and board[0][2] == O:         comp_row = 0        comp_col = 1    elif board[0][0] == * and board[0][1]==O and board[0][2] == O:         comp_row = 0        comp_col = 0    elif board[1][0] == O and board[1][1]==O and board[1][2] == *:        comp_row = 1        comp_col = 2    elif board[1][0] == O and board[1][1]==* and board[1][2] == O:         comp_row = 1        comp_col = 1    elif board[1][0] == * and board[1][1]==O and board[1][2] == O:         comp_row = 1        comp_col = 0    elif board[2][0] == O and board[2][1]==O and board[2][2] == *:        comp_row = 2        comp_col = 2    elif board[2][0] == O and board[2][1]==* and board[2][2] == O:         comp_row = 2        comp_col = 1    elif board[2][0] == * and board[2][1]==O and board[2][2] == O:         comp_row = 2        comp_col = 0    #condition for override (columns)    elif board[0][0] == O and board[1][0] == O and board[2][0] ==*:        comp_row = 2        comp_col = 0    elif board[0][0] == O and board[1][0] == * and board[2][0] ==O:        comp_row = 1        comp_col = 0    elif board[0][0] == * and board[1][0] == O and board[2][0] ==O:        comp_row = 0        comp_col = 0    elif board[0][1] == O and board[1][1] == O and board[2][1] ==*:        comp_row = 2        comp_col = 1    elif board[0][1] == O and board[1][1] == * and board[2][1] ==O:        comp_row = 1        comp_col = 1    elif board[0][1] == * and board[1][1] == O and board[2][1] ==O:        comp_row = 0        comp_col = 1    elif board[0][2] == O and board[1][2] == O and board[2][2] ==*:        comp_row = 2        comp_col = 2    elif board[0][2] == O and board[1][2] == * and board[2][2] ==O:        comp_row = 1        comp_col = 2    elif board[0][2] == * and board[1][2] == O and board[2][2] ==O:        comp_row = 0        comp_col = 2    #condition for override (diagonal)    elif board[0][0] == O and board[1][1] ==O and board[2][2] == *:        comp_row = 2        comp_col = 2    elif board[0][0] == O and board[1][1] ==* and board[2][2] == O:        comp_row = 1        comp_col = 1    elif board[0][0] == * and board[1][1] ==O and board[2][2] == O:        comp_row = 0        comp_col = 0    elif board[0][2] == O and board[1][1] ==O and board[2][0] == *:        comp_row = 2        comp_col = 0    elif board[0][2] == O and board[1][1] ==* and board[2][0] == O:        comp_row = 1        comp_col = 1    elif board[0][2] == * and board[1][1] ==O and board[2][0] == O:        comp_row = 0        comp_col = 2    #if not, back to random generator    else:        if board[0][0] == X and board[0][1]==X and board[0][2] == *:            comp_row = 0            comp_col = 2        elif board[0][0] == X and board[0][1]==* and board[0][2] == X:             comp_row = 0            comp_col = 1        elif board[0][0] == * and board[0][1]==X and board[0][2] == X:             comp_row = 0            comp_col = 0        elif board[1][0] == X and board[1][1]==X and board[1][2] == *:            comp_row = 1            comp_col = 2        elif board[1][0] == X and board[1][1]==* and board[1][2] == X:             comp_row = 1            comp_col = 1        elif board[1][0] == * and board[1][1]==X and board[1][2] == X:             comp_row = 1            comp_col = 0        elif board[2][0] == X and board[2][1]==X and board[2][2] == *:            comp_row = 2            comp_col = 2        elif board[2][0] == X and board[2][1]==* and board[2][2] == X:             comp_row = 2            comp_col = 1        elif board[2][0] == * and board[2][1]==X and board[2][2] == X:             comp_row = 2            comp_col = 0    #condition for override (columns)        elif board[0][0] == X and board[1][0] == X and board[2][0] ==*:            comp_row = 2            comp_col = 0        elif board[0][0] == X and board[1][0] == * and board[2][0] ==X:            comp_row = 1            comp_col = 0        elif board[0][0] == * and board[1][0] == X and board[2][0] ==X:            comp_row = 0            comp_col = 0        elif board[0][1] == X and board[1][1] == X and board[2][1] ==*:            comp_row = 2            comp_col = 1        elif board[0][1] == X and board[1][1] == * and board[2][1] ==X:            comp_row = 1            comp_col = 1        elif board[0][1] == * and board[1][1] == X and board[2][1] ==X:            comp_row = 0            comp_col = 1        elif board[0][2] == X and board[1][2] == X and board[2][2] ==*:            comp_row = 2            comp_col = 2        elif board[0][2] == X and board[1][2] == * and board[2][2] ==X:            comp_row = 1            comp_col = 2        elif board[0][2] == * and board[1][2] == X and board[2][2] ==X:            comp_row = 0            comp_col = 2    #condition for override (diagonal)        elif board[0][0] == X and board[1][1] ==X and board[2][2] == *:            comp_row = 2            comp_col = 2        elif board[0][0] == X and board[1][1] ==* and board[2][2] == X:            comp_row = 1            comp_col = 1        elif board[0][0] == * and board[1][1] ==X and board[2][2] == X:            comp_row = 0            comp_col = 0        elif board[0][2] == X and board[1][1] ==X and board[2][0] == *:            comp_row = 2            comp_col = 0        elif board[0][2] == X and board[1][1] ==* and board[2][0] == X:            comp_row = 1            comp_col = 1        elif board[0][2] == * and board[1][1] ==X and board[2][0] == X:            comp_row = 0            comp_col = 2        else:            while board[comp_row][comp_col] == X or board[comp_row][comp_col] == O:                comp_row = rand_row(board)                comp_col = rand_col(board)    board[comp_row][comp_col] = O    print_board(board)    print     #losing condition    if board[0] == [O,O,O] or board[1] == [O,O,O] or board[2] == [O,O,O]:        print You lose!        break    if (board[0][0] == O and board[1][0] == O and board[2][0] == O) or (board[0][1] == O and board[1][1] == O and board[2][1] == O) or (board[0][2] == O and board[1][2] == O and board[2][2] == O):        print You lose!        break    if (board[0][0] == O and board[1][1] == O and board[2][2] == O) or(board[0][2] == O and board[1][1] == O and board[2][0] == O):        print You lose!        break    if board[0][0] != * and board[0][1] != * and board[0][2] != * and board[1][0] != * and board[1][1] != * and board[1][2] != * and board[2][0] != * and board[2][1] != * and board[2][2] != *:        print Game Over!        break
Naughts and Crosses: Human vs Computer in Python
python;beginner;tic tac toe
The one thing that first strikes me as needing help is the repetition. For example, you have a lot of code like this:    if board[0][0] == O and board[0][1]==O and board[0][2] == *:        comp_row = 0        comp_col = 2    elif board[0][0] == O and board[0][1]==* and board[0][2] == O:         comp_row = 0        comp_col = 1    elif board[0][0] == * and board[0][1]==O and board[0][2] == O:         comp_row = 0        comp_col = 0This could be rewritten like:for i in range(3):    if board[0][i] == * and board[0][(i+1)%3] == O and board[0][(i+2)%3] == O:        comp_row = 0        comp_col = iSo the code for all rows, simplified to a double loop, would look like:for j in range(3)    for i in range(3):        if board[j][i] == * and board[j][(i+1)%3] == O and board[j][(i+2)%3] == O:            comp_row = j            comp_col = iSimilar code could be used for the columns and diagonals.<Edit>This will not quite work, as the if...elif will not work. This will make only the last loop go. I think the solution is to move this logic into a function, that way you can do something like:def check_wins(board, p):    for j in range(3)        for i in range(3):            if board[j][i] == * and board[j][(i+1)%3] == p and board[j][(i+2)%3] == p:                return [j, i]    ...</Edit>Another case of unnecessary repetition can be found in the win condition. three conditions lead to one outcome. this can be simplified to one conditional.    if(        board[0] == [X,X,X] or board[1] == [X,X,X] or board[2] == [X,X,X]     or (board[0][0] == X and board[1][0] == X and board[2][0] ==X) or (board[0][1] == X and board[1][1] == X and board[2][1] ==X) or (board[0][2] == X and board[1][2] == X and board[2][2] ==X)     or (board[0][0] == X and board[1][1] == X and board[2][2] == X) or(board[0][2] == X and board[1][1] == X and board[2][0] == X)    ):        print Congratulations, you win!        break Multi-line conditionals are explained at length in this SO question.next, let us examine the functions.print_board is a function that has a useful purpose that needs to be done many times throughout the game. It is a good function.print_list is used one time, right after it is defined. I think that the code could be better if it were just written inline.rand_row and rand_col' are used enough that they should be functions, although it seems to me thatrandint(0,2)` would be good enough. if you change the board size, that would be the least of your worries.Now for a method that you should add:has_won would be a method that would check every way a victory could happen and tell you if a player has won. this could simplify that code.lastly, for turn in range(20): should be for turn in range(4):. This will work, because 1 move at the beginning plus 4 moves of each player will fill up the board. Except for the fact that you can play on your own space. that could be fixed with:elif board[play_row][play_col] == X:    print You already played on that spot!\nAlso, if you do an invalid move, it should let you try again.Then you can move game over code to the end.