我试图清理我的代码,而不是在函数中使用一堆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())
发布于 2018-06-13 08:31:57
我假设您想知道为什么从上面的代码中得到一个IndexError
(因为其中没有显式的tuple
或索引)。
'{}|{}|{}'.format()
隐含地表示'{0}|{1}|{2}'.format()
,其中0
、1
和2
indicate indices into the positional arguments传递给format
。问题是,您没有传递任何参数,所以当它使用0
对参数的tuple
进行索引时,它会立即失败,引发IndexError
。您需要传递与您在格式字符串中提供的占位符一样多的参数。
https://stackoverflow.com/questions/50827568
复制相似问题