【问题标题】:Tuple index out of range元组索引超出范围
【发布时间】:2015-05-08 22:56:46
【问题描述】:
print "\n\t\t\t\t",moves[6],"|",moves[7],"|",moves[8]

IndexError: 元组索引超出范围

是我得到的错误。我目前正在为tictactoe 编写一个python 程序。移动等于 ['','','','','','','','','']。您还想了解其他信息吗?

到目前为止,我已将行中的 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")

【问题讨论】:

  • 顺便说一句,\n 和 \t 是新行和制表符添加
  • 没有元组索引。您向我们展示了错误的代码。而“\n 和\t 是换行符和制表符”?不可能!
  • 实际上是这样,我目前正在寻找一种方法来粘贴我的代码——我们开始吧!
  • Traceback(最近一次调用最后):文件“C:\Users\\SkyDrive\Documents\recipe-578905-1.py”,第 197 行,在 letsgo(player, ai,移动)文件“C:\Users\\SkyDrive\Documents\recipe-578905-1.py”,第 188 行,在 Letsgo 绘制(移动)文件“C:\Users\\SkyDrive\Documents\recipe-578905-1. py",第 44 行,在绘图中打印 "\n\t\t\t\t",moves[6],"|",moves[7],"|",moves[8] IndexError: tuple index out of range >>> 这些是我没有得到的错误,有人可以解释一下这是怎么回事吗?

标签: python function tuples tic-tac-toe


【解决方案1】:

第 14 行:

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

第 192 行:

moves = XorO(player, ai)
...

    draw(moves)

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

【讨论】:

    【解决方案2】:

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

    IndexError: 元组索引超出范围

    那么错误并没有发生在你说的那一行。 moves 是一个列表,而不是一个元组。它确实具有高达8 的索引。 moves[9] 会产生这个错误:

    IndexError: 列表索引超出范围

    【讨论】:

    • 我实际上是让列表索引超出范围,直到我将其更改为移动[8](从 0 开始)
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-07-28
      • 2021-03-02
      • 2018-11-16
      • 2016-04-04
      • 2020-06-08
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多