![]() |
Tic Tac Toe, Python, code won`t run - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Tic Tac Toe, Python, code won`t run (/thread-29903.html) |
Tic Tac Toe, Python, code won`t run - malinsimone - Sep-25-2020 Hi, I am completely new to programming and am strugling with my assignment Tic Tac Toe in Python code. Can somebody please help me by pointing out my mistakes? I am sure there are lots of mistakes, but I am blind to them and how to solve them. def print_board(board): for row in board: print(row) def check_winner(board, current_player): if board[0][0] == board[0][1] == board[0][2] != "": return True elif board[1][0] == board[1][1] == board[1][2] != "": return True elif board[2][0] == board[2][1] == board[2][2] != "": return True elif board[0][0] == board[1][0] == board[2][0] != "": return True elif board[0][1] == board[1][1] == board[2][1] != "": return True elif board[0][2] == board[1][2] == board[2][2] != "": return True elif board[0][0] == board[1][1] == board[2][2] != "": return True elif board[2][0] == board[1][1] == board[0][2] != "": return True else: return False def update_board(board, row, col, current_player): board[row][col] = current_player def verify_entry(board, row, col): if board[row][col] == " ": return True else: return False if row in range(0,3) and col in range(0,3): return True else: return False board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]] print_board(board) # prints an empty board current = None moves = 0 while moves < 9 and not check_winner(board, current): # game over? # play current = "X" if (current == None or current == "O") else "O" row, col = map(int, input("Enter the move for " + current + ": ").split(",")) while not verify_entry(board, row, col): print("Wrong entry. Think again!") row, col = map(int, input("Enter the move for " + current + ": ").split(",")) update_board(board, row, col, current) print_board(board) # prints the current board moves = moves + 1 if moves < 9: print(current + " WON!") RE: Tic Tac Toe, Python, code won`t run - ndc85430 - Sep-25-2020 It would help if you posted your code within "[python]" tags to keep the indentation and add syntax highlighting, etc. It would also help if you helped us out some more: does the program not do what you expect? If not, what is happening and what have you done to debug it? Are you getting errors? If so, what are they and what do you think they mean? RE: Tic Tac Toe, Python, code won`t run - buran - Sep-25-2020 (Sep-25-2020, 11:10 AM)malinsimone Wrote: I am sure there are lots of mistakes, but I am blind to themInterpreter is pretty good in telling exactly where the error(s) are. What error do you get? post the full traceback in error tags RE: Tic Tac Toe, Python, code won`t run - malinsimone - Sep-25-2020 Thank you very much for adding the propper tags! When I try to run the program i get this
RE: Tic Tac Toe, Python, code won`t run - ndc85430 - Sep-25-2020 So work backwards from that line and figure out why current is None .
RE: Tic Tac Toe, Python, code won`t run - malinsimone - Sep-25-2020 Yes, I have been trying to, but I still don`t get it. Current is set to noen here: current = NoneBut should change here: current = "X" if (current == None or current == "O") else "O"Or am I missing something? RE: Tic Tac Toe, Python, code won`t run - ndc85430 - Sep-25-2020 Well, that line guaranteed to be executed? Hint: it's in a while loop.
RE: Tic Tac Toe, Python, code won`t run - malinsimone - Sep-25-2020 ok. So there is something wrong with my check_winner function? It always returns true... In this line: if board[0][0] == board[0][1] == board[0][2] != "":I try to check if all the positions are equal and not equal to "empty", but is the problem in the last part? How can I check that the position is not empty? Is it becuse I check for an empty sting but should check for something else? RE: Tic Tac Toe, Python, code won`t run - deanhystad - Sep-25-2020 The positions are not empty when you start, they contain spaces. Your game is hard to play. I didn't know how I was supposed to enter a position on the board. Do I enter row column or row, column or column, row or something else. Why do I have to enter two numbers? I don't like to type. Is the top left position 1, 1 or 0, 0? There have been a lot of tic-tac-toe games on the forum and I like the ones that present the board like this: It is pretty clear I should type 5 to put 'X' in the center position.Using positions 1 through 9 also changes the board from a matrix to an array. And you don't have to worry about blank slots because the board starts out full. |