私は.split()文字列メソッドを複製しようとしています。それはうまくいくが、最後の言葉は含まれていない。split()文字列メソッドを使用しない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
テスト1:文=私は私の自転車に乗るのが好き場合は、自分のコードが正しく出力:
['I', 'like', 'to', 'ride', 'my']
私が望む結果は次のとおりです。
['I', 'like', 'to', 'ride', 'my', 'bicycle']
Python for Loopでインデックスを使用している場合は、通常何か問題があります。 – Rosh