2
I made this little program to try to simulate the methods 'split'
and 'rsplit'
of Python
, but when I run 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 choose the separator 'ou'
, it creates the list only with ['pao', 'açucar']
and does not print the last word on the list. I need help finding the bug.
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))
I think I got it. I removed the variable 'word' from the program and each time the first is run I add an empty variable to the list and add the letters of the string until it arrives in the separator. http://prntscr.com/kus0wn
– Bruno Amaral