I'm learning Python at the moment. While taking the first steps, I implemented a Tic Tac Toe game and wanted to share it here, to get some feedback and continue learning. I'm looking for any improvements within my code: style, common mistakes, etc.update: thanks for the valuable input!  below you can find the reworked code, it works well and feels compact and efficient :) still tough, any feedback appreciated!def create_playing_board():    return [[0 for x in xrange(3)] for y in xrange(3)] def print_current_board(board):    print \n,  Y |  , board[0][2], '|', board[1][2], '|', board[2][2]    print    |   --+---+--    print    |  , board[0][1], '|', board[1][1], '|', board[2][1]    print    |   --+---+--    print    |  , board[0][0], '|', board[1][0], '|', board[2][0]    print    |      print    ------------ Xdef set_marker(player, board):    print \nplayer,player,  - x/y input between 0 and 2    x = y = 3    while(x not in range(3)):        x = int(input(x: ))    while(y not in range(3)):            y = int(input(y: ))    if board[x][y] == 0:        board[x][y] = player        return board    else:        print '\nfield already used - choose again!'        set_marker(player,board)        return boarddef check_for_winner(player, board):    if board[0][0] == board[1][1] == board[2][2] == player or \            board[2][0] == board[1][1] == board[0][2] == player:        return True    for x in range(3):        if board[x][0] == board[x][1] == board[x][2] == player or \                board[0][x] == board[1][x] == board[2][x] == player:            return True    return Falsedef check_for_tie(board):    for x in board:        for y in x:            if y == 0:                return False    return Trueboard = create_playing_board()player = 1print welcome to tic tac toewhile(True):    print_current_board(board)        board = set_marker(player, board)    if check_for_winner(player, board) == True:        print ' \n### player', player,' wins! ###\n'        print_current_board(board)        print \ndo you guys want to play again?        if int(input(0 = yes // 1 = no: )) == 1:            break        board = create_playing_board()    if check_for_tie(board) == True:        print \n### TIE! ###        print_current_board(board)        print \ndo you guys want to play again?        if int(input(0 = yes // 1 = no: )) == 1:            break        board = create_playing_board()    player = (player%2)+1im adding here some text as i can not save my edit anymore :( to less text besides the finished code ;)
Python Tic Tac Toe
python;beginner;tic tac toe
Note:This answer has gone through several updates... Some areas are updated to enhance clarity.Although it looks really good, I have a few suggestions.Use print() instead of print if you want this to work with Python 3.xrange(3) is the same as range(0,3)Don't use is_win to determine if the player wants to play again. Use an appropriate variable name. Try considering something like play_again.I actually suggest using while True: and breaking out of the while loop if the user says they don't want to play again instead. But you can make it work either way.When you want to test your code, consider using this code below:if __name__ == '__main__':    # play with stuff I created    # ....For create_playing_board(side_length)I suggest getting rid of side_length because in a simple tic-tac-toe game, it's always 3 x 3.Consider using different variable for the second generator. Like so:    return [[0 for x in range(side_length)] for y in range(side_length)]    # Either this    return [[0 for x in range(3)] for y in range(3)]                         # or this.input() does not cast the user's input into anything except string. So if you want to compare it with an integer, you need to cast it to integer using int().  See the code line below for a quick way to capture and cast into an integer:int(input(A question to ask))When you ask the player if he or she wants to play again and plan on storing their answer into is_win, I don't think that's a really good idea because is_win is already being used for something else. I would consider this instead:print(\nDo you guys want to play again?)        # Ask if the user would like to play again.play_again = int(input(0 = yes // 1 = no: ))Update #1:I just caught a couple of things... How do you determine which player is the winner? -- Never mind.... I found how you did it.The game just keeps on going, even though the board is filled and there is no winner. Try to add something to check for It's a tie!Update #2:I noticed a few more opportunities for improvement...I suggest using boolean values instead of integer to determine one of the two options like yes / no.A way to think about this is:If I want to check for a condition, I just want a Yes or a No.  -- To check for this in programming, it's a True or a FalseSee the code below:# is_win = 0        # Instead of this...is_win = False    # Use this.# .... Somewhere in your code ....# This is just a snippet...def check_for_winner(player, board):    if board[0][0] == board[1][0] == board[2][0] == player:        # return 1    # Instead of this...        return True   # Do this.# .... Somewhere in your code ....# if is_win == 1:   # Instead of this...if is_win:          # Use this...I found way to check for a Tie. I've created a new Gist for this example. Feel free to see how you can implement this check_for_tie() method in your code:def check_for_tie(board):    if not any(0 in sublist for sublist in board):        return True    return FalseIf you plan on implementing #2, consider modifying the code you used to check for is_win to also check for whether there is a tie, and to print the result while minimizing unnecessary redundancy.Think about simplifying the check_for_winner() method. Check this code block below:def check_for_winner(player, board):    # Diagonal    # This is placed up here first because this runs    # slightly quicker than the one in the for loop.    if board[0][0] == board[1][1] == board[2][2] == player or \            board[2][0] == board[1][1] == board[0][2] == player:        return True    # Horizontal or Vertical    for x in range(3):        if board[x][0] == board[x][1] == board[x][2] == player or \                board[0][x] == board[1][x] == board[2][x] == player:            return True    return FalseUpdate #3:Thousands of apologies... I've made quite a list of changes, but I'm just trying to help you out and improve my answer based on Conor Mancone's suggestion.I've updated my Gist and created a new one for comparison.This Gist -- the same one I posted earlier in this answer has my up-to-date suggestion.The 'That' Gist has my alternate suggestion (slight differences).Note:Even though I suggested a laundry list of things in this answer, they are not all implemented in my Gist links; however, they can be taken into reflection. I know this is probably close to a novel, but I hope this helps :)