【问题标题】:Index Error: list index out of range (Python) -- Printing to Console索引错误:列表索引超出范围(Python)——打印到控制台
【发布时间】:2013-04-28 06:28:57
【问题描述】:

我遇到了以下错误:

Traceback (most recent call last):
  File "/Users/joelwilliams/Desktop/delete me", line 30, in <module>
    v.writef( '======================', 10, 10 )
  File "/Users/joelwilliams/Desktop/delete me", line 24, in writef
    self.write( word )
  File "/Users/joelwilliams/Desktop/delete me", line 15, in write
    self.l[ self.y ] [ self.x : ( self.x + len( word ) ) ] = word
IndexError: list index out of range

主要代码在这里:

class board():
    def __init__( self ):
        self.x, self.y = 0, 0
        self.l = []
        self.screenWidth, self.screenHeight = 0, 0

    def createBoard( self ):
        listBig = [ ['`'] * self.screenWidth for _ in range( self.screenHeight ) ]

    def setup( self, sw, sh ):
        self.screenWidth = sw - 1
        self.screenHeight = sh - 1

    def write( self, word ):
        self.l[ self.y ] [ self.x : ( self.x + len( word ) ) ] = word

    def draw( self ):
        for v in self.l:
            print(''.join(v))

    def writef( self, word, y, x ):
        self.cursorPosX = x - 1
        self.cursorPosY = y - 1
        self.write( word )

v = board()
v.setup( 75, 20 )
v.createBoard()

v.writef( '======================', 10, 10 )
v.writef( '=                    =', 11, 10 )
v.writef( '=   Pls Work.        =', 12, 10 )
v.writef( '=                    =', 13, 10 )
v.writef( '======================', 14, 10 )

v.draw()

想要的结果是控制台显示:

           ======================
           =                    =
           =   Pls Work.        =
           =                    =
           ======================

我使用this 作为创建上述代码的指南 提前致谢!

【问题讨论】:

  • 我发誓这是今天最后一个问题:)

标签: python list printing console


【解决方案1】:

在您的createBoard() 方法中:

def createBoard( self ):
    listBig = [ ['`'] * self.screenWidth for _ in range( self.screenHeight ) ]

您正在创建一个包含正确长度和高度的列表,但您从未将其分配给self.l。所以self.l仍然是一个长度为0的列表。

另外,在您的 write() 方法中:

def write( self, word ):
    self.l[ self.y ] [ self.x : ( self.x + len( word ) ) ] = word

看起来你想要self.cursorPosX(和Y)而不是self.xself.y

进行这两项更改,您的程序应该会执行您想要执行的操作。

【讨论】:

    【解决方案2】:

    你的代码

    • 创建一个板(现在是self.l == []
    • 通过两个函数调用设置板,其中一个设置函数局部变量bigList;也许你打算设置self.l(但仍然是self.l == []
    • 设置两个实例变量cursorPosXcursorPosY,它们在其他任何地方都没有被引用;我假设您打算设置 xy(仍然是 self.l == []
    • 尝试检索 self.l 元素的元素(而 self.l == []

    如果你真的在某个地方初始化了self.l,这会有所帮助。我建议将.__init__().setup().createBoard() 合二为一,并且与.write().writef() 类似。像这样的::

    class Board():
      def __init__(self, width, height):
        self.l = [['`'] * (width - 1) for _ in range(height - 1)]
    
      def write(self, text, x, y):
        dx = x + len(text)
        self.l[y][x:dx] = text
    
      def draw(self):
        for row in self.l:
          print(''.join(row))
    

    注意,无用的成员变量screenWidthscreenHeightxycursorPosXcursorPosY都已经被淘汰了。

    要使用这个新代码:

    board = Board(75, 20)
    board.write('======================', 10, 10)
    board.write('=                    =', 11, 10)
    board.write('=   Pls Work.        =', 12, 10)
    board.write('=                    =', 13, 10)
    board.write('======================', 14, 10)
    board.draw()
    

    【讨论】:

    • 我没有注意到所有这些错误。感谢您的帮助并展示了制作我的程序的更好方法!
    猜你喜欢
    • 2020-06-17
    • 2021-05-12
    • 1970-01-01
    • 2018-11-14
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    相关资源
    最近更新 更多