# Spell checker, Version 1.1 # Input: Dictionary of six-letter words and a string entered by the user # Process: determine if the user's string is a word in the dictionary # Output: Indicate YES or NO #infile = open("wordsShort.txt") #wlist = infile.read().split('\n')[:-1] #[:-1] everything but # the last in the list infile = open("wordsShort.txt") wlist=[] word = infile.readline() while word: wlist.append(word[0:len(word)-1]) # strip off the last character, end of line '\n' word=infile.readline() infile.close() print 'Wlist = %s' % wlist str = raw_input('Enter a string: ') #flag = False #for w in wlist: # if w == str: # flag = True # break found = False i = 0 while i < len(wlist) and not found: if str == wlist[i]: found=True else: i=i+1 if found: print "Yes, %s is a word in the list at position %d" % (str,i) else: print "No, %s is not a word in the list." % (str) # Notes # ----- # This version uses a boolean variable and a for loop to determine list # membership. The Python for-loop is like Java's iterator for-loop and # there is no traditional C-style for-loop in Python