Finding specific words in a file (Python language)

Posted by Caroline Yi on Stack Overflow See other posts from Stack Overflow or by Caroline Yi
Published on 2012-03-26T05:09:47Z Indexed on 2012/03/26 5:28 UTC
Read the original article Hit count: 279

Filed under:
|

I have to write a program in python where the user is given a menu with four different "word games". There is a file called dictionary.txt and one of the games requires the user to input a) the number of letters in a word and b) a letter to exclude from the words being searched in the dictionary (dictionary.txt has the whole dictionary). Then the program prints the words that follow the user's requirements. My question is how on earth do I open the file and search for words with a certain length in that file. I only have a basic code which only asks the user for inputs. I'm am very new at this please help :( this is what I have up to the first option. The others are fine and I know how to break the loop but this specific one is really giving me trouble. I have tried everything and I just keep getting errors. Honestly, I only took this class because someone said it would be fun. It is, but recently I've really been falling behind and I have no idea what to do now. This is an intro level course so please be nice I've never done this before until now :(

print
print "Choose Which Game You Want to Play"
print "a) Find words with only one vowel and excluding a specific letter."
print "b) Find words containing all but one of a set of letters."
print "c) Find words containing a specific character string."
print "d) Find words containing state abbreviations."
print "e) Find US state capitals that start with months."
print "q) Quit."
print

choice = raw_input("Enter a choice: ")
choice = choice.lower()
print choice

while choice != "q":
    if choice == "a":
        #wordlen = word length user is looking for.s

        wordlen = raw_input("Please enter the word length you are looking for: ")
        wordlen = int(wordlen)
        print wordlen

        #letterex = letter user wishes to exclude.
        letterex = raw_input("Please enter the letter you'd like to exclude: ")
        letterex = letterex.lower()
        print letterex

© Stack Overflow or respective owner

Related posts about python

Related posts about homework