Python 我能';t使功能完全正确

Python 我能';t使功能完全正确,python,Python,嗨,我真的很困惑在这一个,这是很难让我的一部分'我们做'。只有当我运行代码时,结果才会是['Hooray','Finally'] def split_on_separators(original, separators): """ (str, str) -> list of str Return a list of non-empty, non-blank strings from the original string determined by splitti

嗨,我真的很困惑在这一个,这是很难让我的一部分'我们做'。只有当我运行代码时,结果才会是
['Hooray','Finally']

def split_on_separators(original, separators):
    """ (str, str) -> list of str

    Return a list of non-empty, non-blank strings from the original string
    determined by splitting the string on any of the separators.
    separators is a string of single-character separators.

    >>> split_on_separators("Hooray! Finally, we're done.", "!,")
    ['Hooray', ' Finally', " we're done."]
    """
    #I can't make we're done .
    result = []
    string=''

    for ch in original:
        if ch in separators:
            result.append(string)
            string=''
            if '' in result:
                result.remove('')
        else:
            string+char

     return result           
这一行:

string+char
是在计算某些东西,但不是分配它

请尝试以下方法:

string=string+char
或者,您可以将其缩短为使用
+=
速记:

string += char
这与上述情况相当

    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)
在您的解决方案中,您只测试分离器。因此,当字符串终止时,不会发生任何事情,也不会添加最后一个字符串。您还需要测试字符串终止


还请注意,您没有将当前字符追加到字符串,因此最后一个字符串有一个“.”。也许这就是你想要的(在我看来,它就像一个分隔符)

好吧,我做了这个,但它不起作用。我只是不知道如何分配we's doneWell,您的代码与您在问题中输入的代码不同。我能马上看到的唯一问题是你没有分配任何东西,甚至还有一个bug,因为user3283844有string+char,它甚至没有编译。它是string=string+ch;)实际上,当我应用你所做的时,它才返回[]……但你所做的更合理,但我不知道它为什么返回this@user3283844不,它不返回[]。检查我的代码,有一行字符串=string+ch。您只有string+ch,如果您有这样的字符串,它会返回[]