Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 字符串索引超出范围意味着什么?_Python_Python 3.x - Fatal编程技术网

Python 字符串索引超出范围意味着什么?

Python 字符串索引超出范围意味着什么?,python,python-3.x,Python,Python 3.x,我正在尝试使用python制作一个reversi游戏。这是白板的代码 #reversi import random import sys board = range(9) def char_range(start, end, step=1): for char in range(ord(start), ord(end), step): yield char def drawBoard(board): ###this prints out the board

我正在尝试使用python制作一个reversi游戏。这是白板的代码

   #reversi
import random
import sys
board = range(9)
def char_range(start, end, step=1):
    for char in range(ord(start), ord(end), step):
        yield char
def drawBoard(board):
    ###this prints out the board
    HLINE = ' +-+-+-+-+-+-+-+-+'
    VLINE = ' | | | | | | | | |'
    print('  a b c d e f g h')
    print(HLINE)
    for y in range(8):
        print(y+1, end = '|.|')
        for x in char_range('a','h'):
            print('| %s' %(board[x][y]), end='.')
        print('|')
        print(HLINE)
def resetBoard(board):
    #blanks out the board
    for x in char_range('a', 'h'):
        for y in range(8):
            board[x][y] = '.'
    #starters
    board[c][3] = 'X'
    board[c][4] = 'O'
    board[d][3] = 'O'
    board[d][4] = 'X'
def getNewBoard():
    #new board
    board = []
    for i in range(9):
        board.append([' '] * 9)
    return board
getNewBoard()
drawBoard(board)
resetBoard(board)
出于某种原因,当我执行它时,我收到以下消息:

 File "C:\Users\Larry\Desktop\test.py", line 18, in drawBoard
print('| %s' % (board[x][y]), end='.')
IndexError: string index out of range
这意味着什么。我需要换衣服吗

char_range() 
或者

 print('| %s' % (board[x][y]), end='.')

正如wim所评论的,dict通常是实现这样一个板的完美方式

def getNewBoard():
    #new board
    board = {}
    for x in range(8): # or x in 'abcdefgh': if you prefer
        for y in range(8):
            board[x, y] = ' '
    return board
别忘了将线路板分配给
board

board = getNewBoard()

这意味着字符串没有您想象的那么大,您不能在其结束后编制索引。@derpyherp,dunno您正在执行类似于
board[ord('a')]
?打印(“|%s%”(board[x][y]),end=”)的操作错误:97@derpyherpy:您应该执行
board[x,y]
而不是
board[x][y]
遵循gnibbler的代码。@derpyherp,首先决定你要用哪种方法——字母还是数字。目前,您使用的是一种混合:字母的序数值