Я хочу ограничить количество символов в строке до 77. В связи с этим ограничением, если длина последнего слова превысит 77 символов, я бы хотел разместить его на новой строке ниже текущей строки ,
Я создал код ниже, но он помещает слова «люди» в неправильную строку кода.
txt = '''hello there my dear friends and enemies and other people my name is simon and I like to do drawings for all you happy people'''
txtSplit = []
txtSplit = txt.split(' ')
rowsDict = {}
countOfCharactersPerLine = 0
row = 0
for i in range(len(txtSplit)):
countOfCharactersPerLine += len(txtSplit[i])
if countOfCharactersPerLine >= CHARACTERS_PER_LINE:
countOfCharactersPerLine = len(txtSplit[i])
row += 1
rowsDict[txtSplit[i]] = row
else:
rowsDict[txtSplit[i]] = row
for key,value in rowsDict.items():
print(key,value)
Вывод кода:
hello 0
there 0
my 0
dear 0
friends 0
and 0
enemies 0
other 0
people 1
name 0
is 0
simon 0
I 0
like 0
to 0
do 0
drawings 1
for 1
all 1
you 1
happy 1
Почему слово "люди" стоит в строке 1 вместо строки 0?