I can't make the function completely correct

前端 未结 2 1851
暖寄归人
暖寄归人 2021-01-28 09:22

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\']

相关标签:
2条回答
  • 2021-01-28 09:45

    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.

    0 讨论(0)
  • 2021-01-28 09:47
        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 ;) )

    0 讨论(0)
提交回复
热议问题