元组索引超出范围

Tuple index out of range

print "\n\t\t\t\t",moves[6],"|",moves[7],"|",moves[8]

IndexError: 元组索引超出范围

是我遇到的错误。我目前正在为 tictactoe 编写 python 程序。 moves 等于 ['','','','','','','','','']。您还需要其他信息吗?

到目前为止,我已将行中的 789 更改为 678,因为索引从 0 开始。没有任何反应。

接下来,我尝试了各种其他的小改动,每一个要么改变了错误,要么只给我同样的错误。

我也尝试过更改格式和内容,但效果不佳。我是 运行 python 2.7(?),如果这很重要的话。

def draw(moves):

print "\n\t\t\t\t",moves[6],"|",moves[7],"|",moves[8]
print "\t\t\t\t", "--------"
print "\n\t\t\t\t",moves[3],"|",moves[4],"|",moves[5]
print "\t\t\t\t", "--------"
print "\n\t\t\t\t",moves[0],"|",moves[1],"|",moves[2], "\n"




   import time
import random
moves = ['','','','','','','','','']
player = ''
ai = ''
restart = ''
first = 19
whosfirst = 1
# Ok, so this is to decide the symbol for each person ---------------------------------------------------
def XorO(man, machine):
    print 'Please select a symbol to represent you'
    player = raw_input( "Please select \"X\" or \"O\"")
    while player not in ('x','X','o','O'):
        print "I'm sorry, I don't think I got that. Please try again"
        time.sleep(1)
        player = raw_input("Select \"X\" or \"O\"")
    if player == 'x' or player == 'X':
        print "X"
        time.sleep(1)
        print "Ok, then I'll be \"O\""
        ai = 'o'
    else:
        print "O"
        time.sleep(1)
        print "Ok, then I'll be \"X\""
        ai = 'x'
    return player.upper(), ai.upper()
# This is for who is going first -----------------------------------------------------------------------
def first():
    number = "a"
    while number not in ('n','y',"no",'yes'):
        number = raw_input("Do you want to go first? - ").lower()
        if number == 'y' or number == 'yes':
            return 1
        elif number == 'n' or number == 'no':
            return 0
        else:
            print "I'm sorry, I dont think that I understood you."
            time.sleep(1)
            print "Please select y, yes, n, or no"
#Here Comes the hard part -- drawing the board
def draw(moves):

    print "\n\t\t\t\t",moves[6],"|",moves[7],"|",moves[8]
    print "\t\t\t\t", "--------"
    print "\n\t\t\t\t",moves[3],"|",moves[4],"|",moves[5]
    print "\t\t\t\t", "--------"
    print "\n\t\t\t\t",moves[0],"|",moves[1],"|",moves[2], "\n"

def playerwon():
    print "You won! Yay! Now try again"

def aiwon():
    print "I won! Won't you try again?"

def playerfirst(player, ai, moves):
    while win(player, ai, moves) is None:
        moves = playermove(player, moves - 1)
        moves[int(moves)] = player
        draw(moves)
        if win(player, ai, moves) != None:
            break
        else:
            pass
        Dmove = machine_move(player, ai, moves)
        print "Nice moves! I'll try ",Dmove
        moves[int(Dmove)] = ai
        draw(moves)
    q = win(player, ai, moves)
    if q == 1:
        playerwon()
    elif q == 0:
        aiwon()
    else:
        print "We tied =-("
        time.sleep(2)
        print "Let's have another go!"



def aifirst(player, ai, moves):
    while not win(player, ai, moves):
        Dmove = machine_move(man, machine, moves)
        print "I'll take..." , Dmove
        moves[Dmove] = machine
        draw(moves)
        if win(player, ai, moves) != None:
            break
        else:
            pass
        moves = playermove(player, moves)
        moves[int(moves)] = player
        draw(moves)
    variable = win(player, ai, moves)
    if q == 1:
        playerwon()
    elif q == 0:
        aiwon()
    else:
        print "We tied =-("
        time.sleep(2)
        print "Let's have another go!"


def win(player, ai, moves):
    ways = ((7,8,9),(4,5,6),(1,2,3),(7,4,1)(8,5,2),(9,6,3),(7,5,3),(9,5,1))
    for i in ways:
        if ways[i[0]] == ways[i[1]] == ways[i[2]] != empty:
            winner = new[i[0]]
            if winner == player:
                return 1
            elif winner == ai:
                return 0
            if empty not in new: 
                return 'TIE'
    if empty not in new: 
        return 'TIE'    
    return None



def playermove(player, moves): 
    moves = raw_input("Where would you like to place your symbol?")
    while True:
        if moves not in ('0','1','2','3','4','5','6','7','8'):
            print "Sorry, I don't think that's a valid spot... Try again"
            moves = raw_input("Where would you like to place your symbol ")
        elif moves[int(moves)] != empty:
            print "Sorry, I think there's somehing already there..."
            moves = raw_input("Where would you like to place your symbol?")
        else:
            return int(moves)




def aimove(player, ai, moves):
    bestmoves = [5, 7, 9, 1, 3]
    blank = []
    for i in range(0,9):
        if moves[i] == empty:
            blank.append(i)

    for num in blank:
        moves[i] = ai
        if win(man, ai, moves) is 0:

            return i
        moves[i] = empty

    for num in blank:
        moves[i] = man
        if win(man, ai, moves) is 1:

            return num
        moves[i] = empty

    return int(blank[random.randrange(len(blank))])




def display_instruction():
      print "Welcome to PMARINA's TicTacToe v 1.1.1 ..."
      print "In order to place your symbol, just press the corresponding key on your numpad."
      print "If you do not have a numpad, then please note down the following arrangement"
      print  "7 | 8 | 9"
      print  "-----------"
      print  "4 | 5 | 6"
      print  "-----------"
      print  "1 | 2 | 3"
      print "Good Luck, and don't forget to do the most important thing:"
      time.sleep(2)
      print "HAVING FUN!!"
      time.sleep(2)


def letsgo(player, ai, moves):
    display_instruction()
    print "so lets begin.."
    moves = XorO(player, ai)
    player = moves[0]
    ai = moves[1]
    whosfirst = first()
    if whosfirst == 1:
        print "Ok, you are first!"
        print "Lets go!"
        draw(moves)
        playerfirst(player, ai, moves)
    else:
        print "Ok, I'll be the first!"
        print "So, lets start.."
        draw(moves)
        aifirst(player, ai, moves)


letsgo(player, ai, moves)
raw_input("Press enter to exit")

如果 moves['','','','','','','','',''] 并且您收到的错误消息是:

IndexError: tuple index out of range

那么你说的那行就没有出现错误。 moves 是一个列表,而不是一个元组。它 的索引高达 8moves[9] 会生成此错误:

IndexError: list index out of range

我在代码中看到的唯一元组是方法。此外,在函数 'win' 中使用它之前,您似乎没有初始化 new。 仅供参考,元组使用圆括号 tup = (1, 2, 3, 4) 创建,而列表使用方括号 list = [1,2,3,4]。元组也是不可变的,因此您以后无法修改它们。

第 14 行:

moves = ['','','','','','','','','']

第 192 行:

moves = XorO(player, ai)
...

    draw(moves)

您覆盖了 moves 的初始声明,因此 ('X', 'O') 正在传递给 draw。