为什么这个split()还是没有起作用呢
def break_words(sentence):"""This function will break up words for us."""
words=stuff.split('sentence')
return words
sentence="all good things come to those who wait."
words=break_words(sentence)
words
你的split()函数中的参数应该是你想在哪里分割字符串,而不是你想分割的字符串。在你的代码中,你写的split('sentence')是告诉Python在'sentence'这个词出现的地方分割字符串,而你的字符串并没有包含'sentence'这个词,所以它没有被分割。
另外一个问题是你的函数break_words()中使用的变量是stuff ,但这个变量在函数内部并没有被定义,应该使用的是函数的参数sentence 。
所以,你的函数应该改写成下面这样:
def break_words(sentence):
"""This function will break up words for us."""
words=sentence.split()# 默认使用空格分割字符串
return words
sentence="all good things come to those who wait."
words=break_words(sentence)
print(words)
这样,你的字符串就会按空格被分割成一个个单词,然后作为一个列表返回。 你用sentence切割当然切割不了
还有我想知道你哪来的stuff
页:
[1]