Newbie problem with lists
Jacob H
jacobsmail at gmx.net
Fri Feb 28 03:54:44 EST 2003
Hello everyone..
I'm a relatively inexperienced programmer just starting with Python.
(I like it a lot.) I've been playing with the Pygame module and simple
graphics display. I decided to write a function that would split its
string argument into appropriate lines for the Pygame font module to
display. In doing so, I ran into misunderstandings with lists, I
think, and I'm stumped. Here's the code I wrote for the function:
def render_text(str):
charcount = 0 #how many characters from the current line so
far?
colwidth = 58 #max characters per line
wordlist = string.split(str) #split str into words
text = '' #contents of current line
lines = [] #the output list
y = 10 #tells font where to draw on y axis
for word in wordlist:
wordlen = len(word)
if (wordlen + charcount + 1) > colwidth:
lines.append(text)
text = word + ' '
charcount = wordlen + 1
else:
text = text + word + ' '
charcount = charcount + (wordlen + 1)
for i in range(0, len(lines)):
# print lines[i]
# these last three lines are Pygame stuff and unrelated
# to problem, I think
line = font.render(lines[i], 1, (255, 255, 255))
gamecol.blit(line, (10, y))
y = y + 20
What I expect to get is a list of formatted lines that all display.
Instead, I get only one element to display - as if I'd written print
lines[0]. If anyone could enlighten me as to I'm missing (simple, I'm
sure) I'd be delighted. :)
More information about the Python-list
mailing list