Как исправить ошибку TypeError: unsupported operand type(s) for +=: 'int' and 'str'

inf='v18w17t7Y7w19L13y10c20q18o11z13O7k10I6w7O6u17L8g7v14d18Z8s13w9J4K14'
tmpOut = 0
count=0
char=''
for line in inf:
    if line.isalpha():
        tmpOut += char * int(count)
        char=line
        count=0
    else:
        count+=line
tmpOut += char * int(count)
print(tmpOut)

Ответы (1 шт):

Автор решения: Vadim.Sharoikin

предлагаю сделать так с помощью регулярных выражений. если вывести результат

print(re.findall('(\d+)', inf))

получим

['18', '17', '7', '7', '19', '13', '10', '20', '18', '11', '13', '7', '10', '6', '7', '6', '17', '8', '7', '14', '18', '8', '13', '9', '4', '14']

Далее каждую найденную букву умножаем на элемент в листе

import re

my_str=''
count=0
inf='v18w17t7Y7w19L13y10c20q18o11z13O7k10I6w7O6u17L8g7v14d18Z8s13w9J4K14'
for line in inf:
    if line.isalpha():
        my_str += line*int(re.findall('(\d+)', inf)[count])
        count +=1

print(my_str)

как предложил @insolor

import re

my_str=''
inf='v18w17t7Y7w19L13y10c20q18o11z13O7k10I6w7O6u17L8g7v14d18Z8s13w9J4K14'
for el in re.findall(r'(\w)(\d+)', inf):
    my_str += el[0]*int(el[1])

print(my_str)

можно еще так в одну строку

import re

inf='v18w17t7Y7w19L13y10c20q18o11z13O7k10I6w7O6u17L8g7v14d18Z8s13w9J4K14'
my_str = ''.join([el[0]*int(el[1]) for el in re.findall(r'(\w)(\d+)', inf)])
print(my_str)
→ Ссылка