In: Computer Science
I have the following python code. I get the following message when running it using a test script which I cannot send here:
while textstring[iterator].isspace(): # loop until we get other than space character
IndexError: string index out of range. Please find out why and correct the code.
def createWords(textstrings): createdWords = [] # empty list for textstring in textstrings: # iterate through each string in trxtstrings iterator = 0 begin = iterator # new begin variable while (iterator < len(textstring)): # iterate through each character in the string finished = iterator # new finished character while textstring[iterator].isspace(): # loop until we get other than space character iterator = iterator+1 word = textstring[begin:finished+1].strip() # extract the word from begin position to before the space character, # textstring[begin,finshed] ; strip() will remove white spaces at ends if (word): # if word is not empty createdWords.append(word) # append new word to createWords begin = iterator # update begin value with current iterator position if (textstring[iterator]).isalpha(): if (textstring[iterator].isupper()): # if letter is in uppercase change it to lower case by using letter.lower() method. textstring = textstring[:iterator] + textstring[iterator].lower() + textstring[iterator+1:] print(textstring[iterator],"this a letter") elif (textstring[iterator]).isdigit(): print(textstring[iterator],"is a digit") else: print(textstring[iterator],"this a symbol") word = textstring[begin:finished].strip() # extract the word from begin position to before the symbol character, # textstring[begin,finshed] ; strip() will remove white spaces at ends createdWords.append(word) # append new word to createWords begin = iterator # update begin value with current iterator position iterator= iterator+1 # increment iterator value word = textstring[begin:iterator].strip() # extract the word from begin position to the end of the string createdWords.append(word) # append new word to createWords return createdWords # return createWords
I found below 2 solutions for this. Pick whichever solution works for you.
SOLUTION 1:
While calling the function createWords(textstrings), send a list of sentences like shown below
OUTPUT:
SOLUTION 2:
Remove the first for loop and change the variable name in createwords from "textstrings" to "textstring".
Below is the updated code
OUTPUT:
Code to copy:
def createWords(textstring): createdWords = [] # empty list #for textstring in textstrings: # iterate through each string in trxtstrings iterator = 0 begin = iterator # new begin variable while (iterator < len(textstring)): # iterate through each character in the string finished = iterator # new finished character while textstring[iterator].isspace(): # loop until we get other than space character iterator = iterator+1 word = textstring[begin:finished+1].strip() # extract the word from begin position to before the space character, # textstring[begin,finshed] ; strip() will remove white spaces at ends if (word): # if word is not empty createdWords.append(word) # append new word to createWords begin = iterator # update begin value with current iterator position if (textstring[iterator]).isalpha(): if (textstring[iterator].isupper()): # if letter is in uppercase change it to lower case by using letter.lower() method. textstring = textstring[:iterator] + textstring[iterator].lower() + textstring[iterator+1:] print(textstring[iterator],"this a letter") elif (textstring[iterator]).isdigit(): print(textstring[iterator],"is a digit") else: print(textstring[iterator],"this a symbol") word = textstring[begin:finished].strip() # extract the word from begin position to before the symbol character, # textstring[begin,finshed] ; strip() will remove white spaces at ends createdWords.append(word) # append new word to createWords begin = iterator # update begin value with current iterator position iterator= iterator+1 # increment iterator value word = textstring[begin:iterator].strip() # extract the word from begin position to the end of the string createdWords.append(word) # append new word to createWords return createdWords # return createWords ret = createWords("HELLO, How are you") print(ret)