def find(str, ch): index = 0 while index < len(str): if str[index] == ch: return index index = index + 1 return -1 ## practice file input jabberwocky = open("jabberwok.txt", "r") print jabberwocky line = jabberwocky.read() print line print type(line) jabberwocky = open("jabberwok.txt", "r") line = jabberwocky.readline() # how can you find the beginning and ending of each # word in a line by going through each char and using # find function? # The cat is green wordlist = [] while line != "": print "Next line:", line wdstart = 0 wdend = 0 line2 = line # to find a word, find two spaces - beginning and end while wdstart < len(line): #go through each char in line print "in while at top, wdstart is ",wdstart," and end is ",wdend print "line2 is: ", line2 wordloc = find(line2, " ") #find any specific char print "wordloc is: ", wordloc if wordloc != -1: wdend = wordloc #end of word is at blank word = line[wdstart:wdstart+wdend] else: wdend = len(line)-1 #end of word is at end of line word = line[wdstart:wdend] # wdend = wordloc # beginning of word is at wdstart # end of word is at the space found at wordloc # word = line[wdstart:wdstart+wdend] print "Word is: ", word wordlist = wordlist + [word] print "List is ",wordlist wdstart = wdstart + wdend + 1 line2 = line[wdstart: len(line)] line = jabberwocky.readline()