[This article from contributor Byron translator. If other Nike Jordan Hydro 2 friends also have good original or translations, you can try to recommend to Bole online. ] In this tutorial, I will show how to use Python to do a Tic Tac Toe. This will include the function list, if statements, while loops, for loops, and error handling, and so on. First, we will create two functions, the first function will print out the Tic Tac Toe background template: def Nike Air Max print_board (): for i in range (0,3): for j in range (0,3): print map [2-i] [j], if j = 2:! print \u0026 quot; | \u0026 quot ;, print \u0026 quot; \u0026 quot; Here, we use two for loops to traverse a list of variables called the map. This variable is a two-dimensional 554988 316 Nike Zoom KD V Jade Pink White Sale list, save the information for each location. Since I will follow the keypad numbers to control the position (you will see later), so the first value we set it (2-i), and then we want to use the '|' to split our location Therefore printed after each position, we give him to print a '|', we are here to 579765-700 Sonic Yellow / Sail - Cool Grey - Tour Nike Free 3.0 V4 Yellow Nike Air Max LeBron 10 Low Sonic Yellow Online print map [2-i] [j], the use of commas, to ensure that they are printed on the same line. Now this function can print a background of the game you, it looks like this drop: | | | | | | X | X | O | X | O | O | XX | X | XX | X | XX | X | X Next, we create a check_done () function, which will at the end of each round to check whether the end of the game, if the game is over, then return True and prints a message. def check_done (): for i in range (0,3): if map [i] [0] == map [i] [1] == map [i] [2] = \u0026 quot; \u0026 quot; \\ or map! ! [0] [i] == map [1] [i] == map [2] [i] = \u0026 quot; \u0026 quot ;: print turn, \u0026 quot; won !!! \u0026 quot; return True if map [0] [ 0] == map [1] [1] == map [2] [2] = \u0026 quot;! \u0026 quot; \\ or map [0] [2] == map [1] [1] == map [2] ! [0] = \u0026 quot; \u0026 quot ;: print turn, \u0026 quot; won !!! \u0026 quot; return True if \u0026 quot; \u0026 quot; not in map [0] and \u0026 quot; \u0026 quot; not in map [1] and \u0026 quot; \u0026 quot; not in map [2]: print \u0026 quot; Draw \u0026 quot; return True return False First, we will check the horizontal and vertical directions, Air Max 2011 Womens Grey Red Black is not there three cells are the same, and is not empty (so he would not think three consecutive empty lines are in line with conditions), secondly, we have in the same way to check the diagonal. That if there is a line of eight rows meet the conditions, then the game ends and prints out 'Won !!!' and returns True, at the same time pay attention to turn this variable, its role is to determine which side is now playing chess, it will eventually show up in the news It would be 'X won !!' or 'O won !!.' Next, this function will not determine if a position is empty, then it means that no Nike Lunareclipse one can win (in front of judge this), then print out a draw, and returns True. If not the above two cases, the game is not over, it returns False. OK, now we have two functions, followed by the beginning of our real Air Max 2011 Womens Grey Purple program, the first to create three variables: turn = \u0026 quot; X \u0026 quot; map = [[\u0026 quot; \u0026 quot;, \u0026 quot; \u0026 quot;, \u0026 quot; Air Max 2011 Womens Grey Red Black \u0026 quot;] , [\u0026 quot; \u0026 quot;, \u0026 quot; \u0026 quot;, \u0026 quot; \u0026 quot;], [\u0026 quot; \u0026 quot;, \u0026 quot; \u0026 quot;, \u0026 quot; \u0026 quot;]] Air Jordan Outlet done = False I have already told you these three variables is cooked it mean, if you forget, then look at the following: turn: The Who walked map: game background map Air Max 2011 Womens Grey Purple done: this game in the end there is wood end Next, write:! while done = True: print_board () print turn, \u0026 quot; 's turn \u0026 quot; print moved = False while moved = True:! There is a while loop until done so far is True, we print out whose turn it away. Then create a variable named moved, check the player is not moving, and if there is no movement, then enter the next cycle. Next, we print the player how to go under: print \u0026 quot; Please select position by typing in a number between 1 and 9, see below for which number that is which position ... \u0026 quot; print \u0026 quot; 7 | 8 | 9 \u0026 quot; print \u0026 quot; 4 | 5 | 6 \u0026 quot; print \u0026 quot; 1 | 2 | 3 \u0026 quot; print the following: try: pos = input (\u0026 quot; Select: \u0026 quot;) if pos \u0026 lt; = 9 and pos \u0026 gt; = 1: We hope Players Nike KD 7 Womens enter a number, then we check is not between 1 and 9, at the same time, we have to add an error handler, such as the players enter 'Hello', the program can not just quit. Now, we need to check him taking this 2015 Nike Free 5.0 step can not go: Y = pos / 3X = pos% 3if X = 0:! X - = 1else: X = 2 Y - = 1hah, keep your eyes open anymore. First we get a X and Y values, and then use them to check the position he wants next is not empty, then I will explain to you the X and Y are swollen they work: Position 1: Y = 1/3 = 0 , X = 1% 3 = 1; x - = 1 = 0 position 2: Y = 2/3 = 0, X = 2% 3 = 2; X - = 1 = 1 Location 3: Y = 3/3 = 1 , X = 3% 3 = 0; X = 2, Y - = 1 = 0 ...... following his own count, ah, I direct the conclusion (by, Hexo default template does not display the form ah, I edited at the above time mou more beautiful than the following):! Y \\ Xx = 0x = 1x = 2y = 2789y = 1456y = 0123aha, this location and we type is the same! print \u0026 quot; 7 | 8 | 9 \u0026 quot; print \u0026 quot; 4 | 5 | 6 \u0026 quot; print \u0026 quot; 1 | 2 | 3 \u0026 quot; now we have completed most of the work, but there are a few lines of code: map [Y] [X] = turnmoved = Truedone = check_done () if done == False: if turn == \u0026 quot; X \u0026 quot ;: turn = \u0026 quot; O \u0026 quot; else: turn = \u0026 quot; X \u0026 quot; except: print \u0026 quot; You need to add a numeric value \u0026 quot; ah We moved to the variable copy is True, and check whether an end, the end of the wood, then switch roles replaced a person walking. OK, Nike Running almost over, if you just want to Ctrl + C and Ctrl + V, then, here are all of the code, I hope you learned something, (^ _ ^) / ~ ~ bye. def print_board (): for i in range (0,3): for j in range (0,3): print map [2-i] [j], if j = 2: print \u0026 quot; | \u0026 quot ;, print! \u0026 quot; \u0026 quot; def check_done ():! for i in range (0,3): if map [i] [0] == map [i] [1] == map [i] [2] = \u0026 quot; \u0026 quot ; \\ or map [0] [i] == map [1] [i] == map [2] [i] = \u0026 quot;! \u0026 quot ;: print turn, \u0026 quot; won !!! \u0026 quot; return True if map [0] [0] == map [1] [1] == map [2] [2] = \u0026 quot;! \u0026 quot; \\ or map [0] [2] == map [1] [1] == ! map [2] [0] = \u0026 quot; \u0026 quot ;: print turn, \u0026 quot; won !!! \u0026 quot; return True if \u0026 quot; \u0026 quot; not in map [0] and \u0026 quot; \u0026 quot; not in map [1] and \u0026 quot; \u0026 quot; not in map [2]: print \u0026 quot; Draw \u0026 quot; return True return Falseturn = \u0026 quot; X \u0026 quot; map = [[\u0026 quot; \u0026 quot;, \u0026 quot; \u0026 quot;, \u0026 quot; \u0026 quot;], [\u0026 quot; \u0026 quot; , \u0026 quot; \u0026 quot;, \u0026 quot; \u0026 quot;], [\u0026 quot; \u0026 quot;, \u0026 quot; \u0026 quot;, \u0026 quot; \u0026 quot;]] done = Falsewhile done = True: print_board () print turn, \u0026 quot; 's turn \u0026 quot; print! ! moved = False while moved = True: print \u0026 quot; Please select position by typing in a number between 1 and 9, see below for which number that is which position ... \u0026 quot; print \u0026 quot; 7 | 8 | 9 \u0026 quot; print \u0026 quot ; 4 | 5 | 6 \u0026 quot; print \u0026 quot; 1 | 2 | 3 \u0026 quot; print try: pos = input (\u0026 Air Max 2011 Womens Blue Grey quot; Select: \u0026 quot;) if pos \u0026 lt; = 9 and pos \u0026 gt; = 1: Y = pos / 3 X = ! pos% 3 if X = 0: X - = 1 else: X = 2 Y - = 1 if map [Y] [X] == \u0026 quot; \u0026 quot ;: map [Y] [X] = turn moved = True done = check_done () if done == False: if turn == \u0026 quot; X \u0026 quot ;: turn = \u0026 quot; O \u0026 quot; else: turn = \u0026 quot; X \u0026 quot; except: print \u0026 quot; You need to add a numeric value \u0026 quot;use Python to be a simple game of Tic Tac Toe