首页 > 解决方案 > python中的错误“IndexError:元组索引超出范围”是什么意思?

问题描述

我试图清理我的代码,而不是对函数使用一堆 if 语句。这是我的代码:

def tic(inpinp, board, qweqwe):
    global lik 
    ink = inpinp - 1
    lik = board.insert(ink, qweqwe)


lis = ['  ', '  ', '  ', '  ', '  ', '  ',  '  ', '  ']

p = input("Player 1, would you like to be X or O?")
inpu = int(input("Where would you like to go?"))






print('{}|{}|{}'.format())
print('--|--|--'.format())
print('{}|{}|{}'.format())
print("--|--|--".format())
print('{}|{}|{}'.format())

标签: pythonindex-error

解决方案


我假设您想知道为什么IndexError从上面的代码中得到一个(因为其中没有明确tuple的 s 或索引)。

'{}|{}|{}'.format()隐含的意思是,'{0}|{1}|{2}'.format()其中和指示传递给的位置参数的索引。问题是,你没有传递任何参数,所以当它用 索引参数时,它立即失败,引发. 您需要传递与格式字符串中提供的占位符一样多的参数。012 formattuple0IndexError


推荐阅读