Mar-17-2019, 05:23 PM
Hello,
I've almost got this assignment figured out but now I'm stuck.
The assignment involves prompting the user for a set of words, then the program counts and prints the individual word and the number of letters in each word.
I'm NOT allowed to use split().
I have two variables keeping track of where the words are: left and right
When I run this, it works until the last word and then it doesn't know what to do when right hunts for the next space, there isn't one because it's the end of the list of words.
My teacher puts a lot of emphasis on keeping things simple and he doesn't like it when we get too "pythony", because he's trying to teach us programming in general, not just with python.
MB
I've almost got this assignment figured out but now I'm stuck.
The assignment involves prompting the user for a set of words, then the program counts and prints the individual word and the number of letters in each word.
I'm NOT allowed to use split().
I have two variables keeping track of where the words are: left and right
When I run this, it works until the last word and then it doesn't know what to do when right hunts for the next space, there isn't one because it's the end of the list of words.
My teacher puts a lot of emphasis on keeping things simple and he doesn't like it when we get too "pythony", because he's trying to teach us programming in general, not just with python.
words = input('Enter a series of words with spaces in between: ') print('The length of the input is ',len(words)) wordCount = 0 left = 0 right = 0 while left < len(words): if ' ' not in words[right:]: break elif left >= right: right = words.find(" ", left) wordCount = wordCount + 1 length = right - left + 1 print('len = ',length, words[left:right]) print(left, right) left = right + 1 else: left < right left = left + 1 print('There are ' , wordCount, ' words.')Thanks for any help!
MB