My program freezes (solved)

Hey, I’m really new to Python, and I’m coding super tic tac toe for a school project. Its very unfinished and rough rn with a lot of bugs, but the random number I’m trying to generate for the computer’s turn is just not working and as far as I can tell it’s not my fault? I keep getting this error message after my program freezes and I have to ctrl+c it:

Traceback (most recent call last):
  File "c:\Users\497557626\Desktop\Python\project-1-FloweringPenrose\super_tic-tac-toe.py", line 172, in <module>
    bigComputerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\497557626\Desktop\Python\project-1-FloweringPenrose\super_tic-tac-toe.py", line 95, in bigComputerMove
    boardNum = random.randint(1,9)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.1008.0_x64__qbz5n2kfra8p0\Lib\random.py", line 340, in randint
    return self.randrange(a, b+1)
           ~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.1008.0_x64__qbz5n2kfra8p0\Lib\random.py", line 322, in randrange
    return istart + self._randbelow(width)
                    ~~~~~~~~~~~~~~~^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.1008.0_x64__qbz5n2kfra8p0\Lib\random.py", line 245, in _randbelow_with_getrandbits
    def _randbelow_with_getrandbits(self, n):

KeyboardInterrupt

I’m using Visual Studio Code and I’ve already tried restarting the program multiple times, but it doesn’t seem to do anything and I can’t find anyone else with this problem. I can post the rest of my code if needed, but I dont think thats the problem?

When you press Ctrl-C, a KeyboardInterrupt happens at whatever point Python happens to be at. That’s not really a problem necessarily; what it means, though, is that your code wasn’t stuck, it was spinning.

One thing you’ll learn as you write Python code (or any other good high-level language) is that exception tracebacks are great at telling you where a bug was FOUND, but sometimes that isn’t where the actual problem is. It often will be (or at least close to that), but sometimes it’s not. In this case, the real question is: Why is your code spinning? Most likely, there’s some logic error that is resulting in an infinite loop.

So it actually will help a lot to see the rest of your code; the problem is almost certainly in there somewhere, and we can help you track it down.

By the way, thank you for this level of detail:

This was incredibly helpful. Giving an explanation of what happened - and including the traceback - is the best way to help us to help you find the problem. :slight_smile: With the rest of the code, I am confident that we’ll be able to point you to the bug!

1 Like

Oh thank you that makes a lot of sense! This is the rest of my code:


#      7    |    8    |    9
#    o|x|o  |  o|x|o  |  o|x|o
#   --|-|-- | --|-|-- | --|-|--
#    o|x|o  |  o|x|o  |  o|x|o
#   --|-|-- | --|-|-- | --|-|--
#    o|x|o  |  o|x|o  |  o|x|o
# ----------|---------|-----------
#      4    |    5    |    6
#    o|x|o  |  o|x|o  |  o|x|o
#   --|-|-- | --|-|-- | --|-|--
#    o|x|o  |  o|x|o  |  o|x|o
#   --|-|-- | --|-|-- | --|-|--
#    o|x|o  |  o|x|o  |  o|x|o
# ----------|---------|-----------
#      1    |    2    |    3
#    o|x|o  |  o|x|o  |  o|x|o
#   --|-|-- | --|-|-- | --|-|--
#    o|x|o  |  o|x|o  |  o|x|o
#   --|-|-- | --|-|-- | --|-|--
#    o|x|o  |  o|x|o  |  o|x|o
#           |         |

import random
board1 = [""]+[" "]*9
board2 = [""]+[" "]*9
board3 = [""]+[" "]*9
board4 = [""]+[" "]*9
board5 = [""]+[" "]*9
board6 = [""]+[" "]*9
board7 = [""]+[" "]*9
board8 = [""]+[" "]*9
board9 = [""]+[" "]*9
board = ["", board1, board2, board3, board4, board5, board6, board7, board8, board9]
player = "x"
computer = "o"
turn = "player"
boardNum = ""
move = ""
temp = 1

def drawBoard(board1, board2, board3, board4, board5, board6, board7, board8, board9):
    print("      7    |    8    |    9")
    print("    " + board7[7] + "|" + board7[8] + "|" + board7[9] + "  |  " + board8[7] + "|" + board8[8] + "|" + board8[9] + "  |  " + board9[7] + "|" + board9[8] + "|" + board9[9])
    print("   --|-|-- | --|-|-- | --|-|--")
    print("    " + board7[4] + "|" + board7[5] + "|" + board7[6] + "  |  " + board8[4] + "|" + board8[5] + "|" + board8[6] + "  |  " + board9[4] + "|" + board9[5] + "|" + board9[6])
    print("   --|-|-- | --|-|-- | --|-|--")
    print("    " + board7[1] + "|" + board7[2] + "|" + board7[3] + "  |  " + board8[1] + "|" + board8[2] + "|" + board8[3] + "  |  " + board9[1] + "|" + board9[2] + "|" + board9[3])
    print(" ----------|---------|-----------")
    print("      4    |    5    |    6")
    print("    " + board4[7] + "|" + board4[8] + "|" + board4[9] + "  |  " + board5[7] + "|" + board5[8] + "|" + board5[9] + "  |  " + board6[7] + "|" + board6[8] + "|" + board6[9])
    print("   --|-|-- | --|-|-- | --|-|--")
    print("    " + board4[4] + "|" + board4[5] + "|" + board4[6] + "  |  " + board5[4] + "|" + board5[5] + "|" + board5[6] + "  |  " + board6[4] + "|" + board6[5] + "|" + board6[6])
    print("   --|-|-- | --|-|-- | --|-|--")
    print("    " + board4[1] + "|" + board4[2] + "|" + board4[3] + "  |  " + board5[1] + "|" + board5[2] + "|" + board5[3] + "  |  " + board6[1] + "|" + board6[2] + "|" + board6[3])
    print(" ----------|---------|-----------")
    print("      1    |    2    |    3")
    print("    " + board1[7] + "|" + board1[8] + "|" + board1[9] + "  |  " + board2[7] + "|" + board2[8] + "|" + board2[9] + "  |  " + board3[7] + "|" + board3[8] + "|" + board3[9])
    print("   --|-|-- | --|-|-- | --|-|--")
    print("    " + board1[4] + "|" + board1[5] + "|" + board1[6] + "  |  " + board2[4] + "|" + board2[5] + "|" + board2[6] + "  |  " + board3[4] + "|" + board3[5] + "|" + board3[6])
    print("   --|-|-- | --|-|-- | --|-|--")
    print("    " + board1[1] + "|" + board1[2] + "|" + board1[3] + "  |  " + board2[1] + "|" + board2[2] + "|" + board2[3] + "  |  " + board3[1] + "|" + board3[2] + "|" + board3[3])
    print("           |         |")

def startGame():
    global player
    global computer
    player = input("do you want to be x or o? ")
    if(player != "o" or "x"):
        player = input("thats not an option. do you want to be x or o? ")
    if(player == "o"):
        computer = "x"

def bigPlayerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9):
    global boardNum
    global player
    global move
    global turn
    boardNum = int(input("Which game would you like to play in? "))
    move = int(input("Which space would you like to play in? "))
    board[boardNum][move] = player
    turn = "player"
    drawBoard(board1, board2, board3, board4, board5, board6, board7, board8, board9)

def bigComputerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9):
    global boardNum
    global computer
    global move
    global turn
    boardNum = random.randint(1,9)
    move = random.randint(1,9)
    while(board[boardNum][move] == "x", "o"):
        boardNum = random.randint(1,9)
        move = random.randint(1,9)
    board[boardNum][move] = computer
    turn = "computer"
    drawBoard(board1, board2, board3, board4, board5, board6, board7, board8, board9)

def nextGame():
    global boardNum
    global move
    boardNum = move

def smallPlayerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9):
    global move
    global player
    global boardNum
    global turn
    boardNum = move
    move = int(input("You are playing in board " + str(boardNum) + ", which space would you like to play in? "))
    board[boardNum][move] = player
    turn = "player"
    drawBoard(board1, board2, board3, board4, board5, board6, board7, board8, board9)

def smallComputerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9):
    global move
    global computer
    global boardNum
    global turn
    boardNum = move
    move = random.randint(1,9)
    while(board[boardNum][move] == "x" or "o"):
        move = random.randint(1,9)
    board[boardNum][move] = computer
    turn = "computer"
    drawBoard(board1, board2, board3, board4, board5, board6, board7, board8, board9)

def checkSmallWin(board):
    global boardNum
    if((board[boardNum][7] == board[boardNum][8] == board[boardNum][9] != " ") or
	(board[boardNum][4] == board[boardNum][5] == board[boardNum][6] != " ") or
	(board[boardNum][1] == board[boardNum][2] == board[boardNum][3] != " ") or
	(board[boardNum][1] == board[boardNum][4] == board[boardNum][7] != " ") or
	(board[boardNum][2] == board[boardNum][5] == board[boardNum][8] != " ") or
	(board[boardNum][3] == board[boardNum][6] == board[boardNum][9] != " ") or
	(board[boardNum][7] == board[boardNum][5] == board[boardNum][3] != " ") or
	(board[boardNum][9] == board[boardNum][5] == board[boardNum][1] != " ")):
        if(turn == "player"):
            winner = player
        else:
            winner = computer
        board[boardNum] = winner
    else: 
        return False

def checkBigWin(board):
    if((board[7] == board[8] == board[9]) or
	(board[4] == board[5] == board[6]) or
	(board[1] == board[2] == board[3]) or
	(board[1] == board[4] == board[7]) or
	(board[2] == board[5] == board[8]) or
	(board[3] == board[6] == board[9]) or
	(board[7] == board[5] == board[3]) or
	(board[9] == board[5] == board[1])):
        return True
    else:
        return False



startGame()
drawBoard(board1, board2, board3, board4, board5, board6, board7, board8, board9)
bigPlayerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9)
checkBigWin(board)
while(temp == 1):
    nextGame()
    checkSmallWin(board)
    print(boardNum)
    if(board[boardNum] == "x" or "o"):
        bigComputerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9)
        nextGame()
        checkSmallWin(board)
        if(board[boardNum] == "x" or "o"):
            bigPlayerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9)
        else:
            smallPlayerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9)
            nextGame()
    else:
        smallComputerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9)
        nextGame()
        checkSmallWin(board)
        if(board[boardNum] == "x" or "o"):
            bigPlayerMove(board, board1, board2, board3, board4, board5, board6, board7, board8, board9)
    if(checkBigWin(board) == True):
        break
print("game over :)")

The comments at the beginning are how the board should look when printed. This is also not the only bug I’m aware of in the program, but this is the one I’m working on right now.

Okay, couple of things you may want to consider - this is stuff that I noticed along the way, but isn’t your core problem.

See if you can figure out a way to use a for loop, and possibly a list comprehension, to simplify this code. It’s quite repetitive and follows a simple pattern, so it’s a great opportunity to let the computer do the boring part for you.

This doesn’t do what you think it does. The or keyword will look at everything to its left, and everything to its right. So you’re asking player != "o"? and if that’s not the case, then "x"? Python doesn’t magically remember that you’re comparing against player, so it’ll just ask whether "x" is a thing (which it is). Also, you prompt once more, but then accept whatever was entered. Input validation is a great use for a while loop, since you want to keep looping so long as the input isn’t valid (or, wording it another way, you want to loop until the input is valid).

So, here’s the actual problem.

I can see what you’re going for here, but it isn’t what Python understands this to mean. Python looks at it as a tuple. If we look at it as a list instead, it might be clearer what’s happening:

[board[boardNum][move] == "x", "o"]

Perhaps it’s now more obvious what’s happening: you compare the board with "x", and then build up a list with that (either True or False) and "o". This two-item tuple is always going to be truthy, so the while loop will continue forever.

What you want, instead (and the same will be useful in your input validation), is to ask whether the board element is one of a collection. You can write that like this:

while board[boardNum][move] in ("x", "o"):

The in operator asks whether the thing on its left can be found among the things on the right.

Hope that helps!

3 Likes