Reputation: 118
I tried to limit the range "i" could go to with:
while i < len(words) - 1
But this doesn't look to get my problem fixed since I get the error:
in corta_texto
while size + len(words[i]) + len(phrase_line) - 1 < maxlength:
IndexError: list index out of range
I can't quite get it to work as I've tried to poke here and there to fix it. Im quite new to python and asking here would be my last resource because this is a school project but I really can't seem to get this to work.
Here's the code so far:
def corta_texto(phrase, maxlength):
words = phrase.split()
phrase_tuple = []
phrase_line = ''
size = 0
i = 0
while i < len(words) - 1:
while size + len(words[i]) + len(phrase_line) - 1 < maxlength:
size += len(words[i]) + len(phrase_tuple) - 1
phrase_line += words[i] + ' '
i += 1
else:
phrase_tuple.append(phrase_line)
phrase_line = ''
i += 1
size = 0
return phrase_tuple
I've tried I've looked for other questions like this but I can't seem to figure out what's wrong with my code. Any help would be appreciated.
Upvotes: 0
Views: 76
Reputation: 88
In Python, you should use the 'in' statement for lists, arrays, etc.
words = ['word', 'word2', 'word3']
for word in words:
print(word)
Output:
word
word2
word3
Upvotes: 2