user7733823
user7733823

Reputation:

Tic-Tac-Toe Error handling Python

I've almost completed my Tic-Tac-Toe game. However, when the user inputs a number greater than 33, I get the following error:

     while board[choice] != " ":
"IndexError: list index out of range"

My list only goes from 0-33. I'm not sure how to prevent this error if say 50 is entered by mistake.

ty

#Defining the board (The board is initally an empty list, which evenutally holds the user's data/input.
#The desired user input of a x(column) and y(row) coordinate "1,2,3 by 1,2,3" is held by the associated list value point.
#For example, "board[31] will hold either "X" or "O" when the user enters "31" (column 3, row 1) 
board = ["", "", "", "", "", "", "", "", "", "", "", " ", " ", " ", "", "", "", "", "", "", "", " ", " ", " ", "", "", "", "", "", "", "", " ", " ", " "]

#Defining the print_board function. This produces/prints the actual board in real-time for the console when needed. (When a user chooses an available block)
def print_board():
    print (' 1     2    3   ')
    print ('     |    |   ')
    print ('1  '+board[11]+" | "+board[21]+"  | "+board[31]+"")
    print (' ----+----+----')
    print ('     |    |   ')
    print ('2  '+board[12]+" | "+board[22]+"  | "+board[32]+"")
    print ('     |    |   ')
    print (' ----+----+----')
    print ('     |    |   ')
    print ('3  '+board[13]+" | "+board[23]+"  | "+board[33]+"")
    print ('     |    |   ')

#Welcome message and instructions.  
print("Welcome to Tic-Tac-Toe\n-----------------------\n")    
print("Please enter a column followed by a row, e.g, 22 equates to the middle box.")
print("   1    2    3  ")
print("1    |     |    ")
print("-----+-----+----")
print("2    |  x  |    ")
print("-----+-----+----")
print("3    |     |    ")
print("-----|-----|----\n\n")



#while True statement to start the game.
while True:
    print_board()
    #Firstly print the board

    #Player "X"
    #Player "X" input 
    while True:
        try:
            choice = int(input("Please choose an empty space for X. (column,row) ")) 
            break
        except ValueError:
            print ("Error, Please enter a coordinate.")

        else:
             break


    #While loop, in case location is already taken.
    while board[choice] != " ":
            choice = int(input("Please choose an empty space for X. (column,row) "))
    if board[choice] == " ":
        board[choice] = "X"


    #Possible combinations for "X" to win.      
    if  board[11] == "X" and board[21] =="X" and board[31] == "X" or \
        board[12] == "X" and board[22] =="X" and board[32] == "X" or \
        board[13] == "X" and board[23] =="X" and board[33] == "X" or \
        board[11] == "X" and board[12] =="X" and board[13] == "X" or \
        board[21] == "X" and board[22] =="X" and board[23] == "X" or \
        board[31] == "X" and board[32] =="X" and board[33] == "X" or \
        board[11] == "X" and board[22] =="X" and board[33] == "X" or \
        board[31] == "X" and board[22] =="X" and board[13] == "X":
        print_board()
        print ("X wins! Congratulations")
        break
    #Displays the last move.
    print_board()

    # True statement to identify a tie. If all locations are occupied then the game will result in a tie.
    # isFull = True, is assuming the board is already full and a empty space " " will DISPROVE the True statement.
    isFull = True
    if " " in board:
        isFull = False

    if isFull == True:
        print ("Tie")
        break


    #Player "O"
    #Player "O" input 
    while True:
        try:
            choice = int(input("Please choose an empty space for O. (column,row) "))
            break
        except ValueError:
            print ("Error")

        else:
             break


    #While loop, in case location is already taken.
    while board[choice] != " ":
            choice = int(input("Please choose an empty space for O. (column,row) "))
    if board[choice] == " ":
        board[choice] = "O"


    #Possible combinations for "O" to win. 
    if  board[11] == "O" and board[21] =="O" and board[31] == "O" or \
        board[12] == "O" and board[22] =="O" and board[32] == "O" or \
        board[13] == "O" and board[23] =="O" and board[33] == "O" or \
        board[11] == "O" and board[12] =="O" and board[13] == "O" or \
        board[21] == "O" and board[22] =="O" and board[23] == "O" or \
        board[31] == "O" and board[32] =="O" and board[33] == "O" or \
        board[11] == "O" and board[22] =="O" and board[33] == "O" or \
        board[31] == "O" and board[22] =="O" and board[13] == "O":
        print_board()
        print ("O wins! Congratulations")
        break

    # True statement to identify a tie. If all locations are occupied then the game will result in a tie.
    # isFull = True, is assuming the board is already full and a empty space " " will DISPROVE the True statement.
    isFull = True
    if " " in board:
        isFull = False

    if isFull == True:
        print ("Tie")
        break

Upvotes: 0

Views: 444

Answers (1)

Let'sCode
Let'sCode

Reputation: 55

Try adding this after line 41 where you ask the user to input:

while choice > 33 or choice < 0:
    print('Invalid input. Try again.')
    choice = int(input())

It checks if the input is outside the range of 0 to 33 and, if so, asks the user to input again.

Upvotes: 1

Related Questions