I made this little program to try to simulate the methods 'split'
and 'rsplit'
of Python
, but when I run it it does not print the last sentence of the string. For example, if in the string I type 'pao ou açucar ou café'
and I choose the 'ou'
tab, it creates the list with only ['pao', 'açucar']
and does not print the last word in the list. I need help finding the error.
def separa(string):
separador = input("Digite o separador: ")
lista = []
x = 0
for i in range(string.count(separador)+1):
palavra = ''
for j in range(x, len(string)):
if string[j:j+len(separador)] == separador:
lista.append(palavra)
x = j+len(separador)
break
palavra += string[j]
return lista
string = input("Digite a string: ")
print(separa(string))