Kim Y
Kim Y

Reputation: 79

easy python program, not easy for me

I have to write a program that has four different word game options.

One of the options is that the user inputs a certain length of a word from a dictionary file and then the program finds a word with that length that can be made up of the abbreviations for the 50 states of USA.

Ex: if the user inputs 6, then the program finds the word MARINE, which is made up of 5 abbreviations: MA (Massachusetts), AR (Arizona), RI (Rhode Island), IN (Indiana), and NE (Nebraska).

I have this so far:

elif choice == "d":
    #wordlen = length of words user is looking for.
    wordlen = raw_input("Enter the length of words you are looking for: ")
    wordlen = int(wordlen)
    print wordlen

    states = ['AL','AK','AZ','AR','CA','CO','CT','DE','DC','FL','GA','HI',\
    'ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT',\
    'NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD',\
    'TN','TX','UT','VT','VA','WA','WV','WI','WY']

    for line in file:
        line = line.strip()
        line = line.upper()

        if len(line) == wordlen:

I have already opened the dictionary file (file = file.open('dictionary.txt') at the beginning of the whole loop and used file.seek(0) and then when the loop is broken (when the user inputs 'q'), file.close() so that I don't have to open and close the file during each procedure.

And after I have the condition that the user inputs, I don't know what to do. I've tried everything and it doesn't give me the expected output. Help please? This is my first year taking python and it's very confusing to me -__-

Upvotes: 3

Views: 362

Answers (1)

ely
ely

Reputation: 77504

For each line in the dictionary file that has the correct length (e.g. matches the user input length), you need to check each sequential pair of letters in that word to see if in appears in the states list.

for line in file:
    line = line.strip()
    line = line.upper()

    if len(line) == wordlen:

        # Set some counters/containers.
        pair_is_abbrev = 1 #<-- Marks true/false if pair is in abbrev list.
        cur_letter_indx = 0 #<-- Counts the location we're at in this line.

        # Loop until we find a two-letter sequence that's not an abbrev.
        # or until we hit the end of this line (the word length).
        while(pair_is_abbrev and cur_letter_indx <= wordlen-1):
            cur_pair = line[cur_letter_indx:(cur_letter_indx+2)] #<-- Get current two letters
            pair_is_abbrev = (cur_pair in states) #<-- Python way to check if that pair is in your list of abbrevs.
            cur_letter_indx = cur_letter_indx + 1 #<-- Increment the counter.

        # Once the loop terminates, pair_is_abbrev can only be true if we
        # made it all the way to the end of the line successfully. If so,
        # then we found an all-abbrevs word. Otherwise, move on to the next line.
        if(pair_is_abbrev):
            print "Found a word made of abbreviations that is the right length:"
            print line

Upvotes: 4

Related Questions