【发布时间】:2017-06-10 05:44:40
【问题描述】:
我正在尝试复制 .split() 字符串方法。它运作良好,但不包括最后一个字。
def stringSplitter(string):
words = []
current_word = ""
for x in range(len(string)): #problem is here
if string[x] == " ":
words.append(current_word)
current_word = ""
else:
current_word += string[x]
return words
测试一:当句子=我喜欢骑自行车时,我的代码输出错误:
['I', 'like', 'to', 'ride', 'my']
我想要的结果是:
['I', 'like', 'to', 'ride', 'my', 'bicycle']
【问题讨论】:
-
如果你在你的 python for 循环中使用索引,你通常做错了。