I am trying to construct a method that takes in a phrase and splits it according to its spaces and then returns a list of words.
Example:
- Input: 'Hello there how are you'
- Output: ['Hello', 'there', 'how', 'are', 'you']
Currently, I am getting this (the last word is missing), because it will not append the last word as long as it can't find a space after it
- Output: ['Hello', 'there', 'how', 'are']
I know that I can do this through the split method but I just wanted to know if I could do the same thing using my code.
def words(phrase):
my_list = []
final_word = ''
for word in phrase:
if word == ' ':
my_list.append(final_word)
final_word = ''
else:
final_word = final_word + word
return my_list