Python Forum
Tic Tac Toe, Python, code won`t run
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tic Tac Toe, Python, code won`t run
#1
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!")
Reply
#2
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?
Reply
#3
(Sep-25-2020, 11:10 AM)malinsimone Wrote: I am sure there are lots of mistakes, but I am blind to them
Interpreter is pretty good in telling exactly where the error(s) are. What error do you get? post the full traceback in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thank you very much for adding the propper tags!

When I try to run the program i get this

Error:
[' ', ' ', ' '] [' ', ' ', ' '] [' ', ' ', ' '] Traceback (most recent call last): File "C:\Prosjekt\Git projects\tictactoe\tic_tac_toe_template.py", line 92, in <module> print(current + " WON!") TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' >>>
Reply
#5
So work backwards from that line and figure out why current is None.
Reply
#6
Yes, I have been trying to, but I still don`t get it.

Current is set to noen here:
current = None
But should change here:

current = "X" if (current == None or current == "O") else "O"
Or am I missing something?
Reply
#7
Well, that line guaranteed to be executed? Hint: it's in a while loop.
Reply
#8
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?
Reply
#9
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:
Output:
1 2 3 4 5 6 7 8 9 Enter position (1-9) for X:
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020