0

我遇到了以下错误:

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.        =
           =                    =
           ======================

以此作为创建上述代码的指南,在此先感谢!

4

2 回答 2

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(and Y) 那里而不是self.xand self.y

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

于 2013-04-28T06:43:15.687 回答
1

你的代码

  • 创建一个板(现在self.l == []
  • sets up the board over two function calls, one of which sets a function-local variable bigList; maybe you meant to set self.l (but still self.l == [])
  • sets two instance variables cursorPosX and cursorPosY which are not referenced anywhere else; I'll assume you meant to set x and y (and still self.l == [])
  • attempts to retrieve an element of an element of self.l (while self.l == [])

It would help if you actually initialized self.l somewhere. I suggest rolling .__init__(), .setup() and .createBoard() into one, and similarly with .write() and .writef(). Something like this::

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))

Note that the useless member variables screenWidth, screenHeight, x, y, cursorPosX, and cursorPosY have all been eliminated.

To use this new code:

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()
于 2013-04-28T06:48:15.460 回答