0

我几乎完成了我的井字游戏。但是,当用户输入大于 33 的数字时,我收到以下错误:

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

我的名单只有 0-33。如果错误地输入了 50,我不确定如何防止此错误。

#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
4

1 回答 1

1

尝试在要求用户输入的第 41 行之后添加:

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

它检查输入是否超出 0 到 33 的范围,如果是,则要求用户再次输入。

于 2017-10-12T09:03:49.353 回答