我遇到了以下错误:
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. =
= =
======================
我以此作为创建上述代码的指南,在此先感谢!