So I have this assignment for a class where I have to split user input text into words and then into list items.(we are not allowed to use the inbuilt split function) I came up with a solution that just goes through every character and combines them if there isn't a space in between.
def my_split(sentence,seperator):
list = []
newStr = ""
for i in range(len(sentence)):
if sentence[i].isalpha():
newStr += sentence[i]
else:
list.append(newStr+seperator)
newStr = ""
print(list)
def main():
while True:
anws = input("Write the sentence:")
my_split(anws,",")
break
if __name__ == "__main__":
main()
The code almost works, except it always leaves the last word out for some reason. How can I fix that?
EDIT: Lots of comments about the seperator...I misunderstood that part of the assignment :D it's supposed to point out what we are looking for between the words(the space).