Hi I am really confused in this one, it is very hard for me to make the part \'we\'re done\'. Only when I run the code, the result would only be [\'Hooray\', \' Finally\']
This line:
string+char
is computing something, but not assigning it.
Try this instead:
string=string+char
Or, you can shorten it to use +=
shorthand:
string += char
Which is equivilent to the above.
def split_on_separators(original, separators):
result = []
string=''
for index,ch in enumerate(original):
if ch in separators or index==len(original) -1:
result.append(string)
string=''
if '' in result:
result.remove('')
else:
string = string+ch
return result
res = split_on_separators("Hooray! Finally, we're done.", "!,")
print(res)
In your solution, you only test for separators. So when the string is terminated, nothing happens and the last string is not added. You need to test for string termination as well.
Please also take note that you are not appending the current character to the string, so the last string has a ".". Maybe it's what you want (looks like a separator to me ;) )