954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What's wrong with this code?

I'm new to Python and this was written and posted by someone else on another site as a tutorial for a word unscrambler. I thought it was well written and liked the explanations but I tried to use it but keep getting an error message:
line 27, in
letters = list(wordlist[x])
IndexError: list index out of range
Can someone explain this to me and tell me how to correct it? Here's the code below. Written by '15feb93':

import string
dictString = open('wordlist.txt', 'U').read() 
dictList = dictString.split('\n') 
 
x = 0
newDict = ''
while x != 1275:
letters = list(dictList[x])
letters.sort()
abcString = str(letters).strip(" [] ").replace(',', '').replace("\'", '').replace(' ', '')
newDict += abcString + '\n'
x += 1
abcDictList = newDict.split('\n')
x = 0
wordstring = open('scrambled.txt', 'U').read()
wordstring = wordstring.replace('#', '').replace('\t', '').replace(' ', '').replace(',', '\n')
wordlist = wordstring.split('\n')
newWord = ''
while x != 10:
letters = list(wordlist[x])
letters.sort()
abcString = str(letters).strip(" [] ").replace(',', '').replace("\'", '').replace(' ', '')
newWord += abcString + '\n'
x += 1
abcWordList = newWord.split('\n')
x = 0
Solution = ''
while x != 10:
y = 0
while y != 1275:
if abcWordList[x] == abcDictList[y]:
break
else:
y += 1
Solution += dictlist[y] + ','
x += 1
solutionWrite = open('solution.txt', 'w')
solutionWrite.write(Solution)
defience
Newbie Poster
17 posts since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

Right now the main thing wrong are all the missing indentation that Python uses to block code.

It would be nice to know what 'wordlist.txt' looks like.

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

Thanks for the reply, I have the indentations although when I posted the code on here it seems to have taken them out. As far as the wordlist goes, there are 1275 words in it "while x != 1275:", and it is in my python directory.

defience
Newbie Poster
17 posts since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

So your file has one word per line?

The missing indentations are most like fault of your code editor.
What editor are you using?

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

So your file has one word per line?

The missing indentations are most like fault of your code editor. What editor are you using?

Yes the file has one word per line. The indentations are there on my code, just seems that when I copied it and pasted it to this forum that they're not showing. I tried to edit the code on here but it's apparently too late for that but the indents are there.

defience
Newbie Poster
17 posts since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

I don't have your files available to test this, but I added try/excepts just in case you have less data in your files than you expect ...

import string
dictString = open('wordlist.txt', 'U').read()
dictList = dictString.split('\n') 
x = 0
newDict = ''
while x != 1275:
    try:
        letters = list(dictList[x])
    except IndexError:
        break
    letters.sort()
    abcString = str(letters).strip(" [] ").replace(',', '').replace("\'", '').replace(' ', '')
    newDict += abcString + '\n'
    x += 1
abcDictList = newDict.split('\n')
x = 0
wordstring = open('scrambled.txt', 'U').read()
wordstring = wordstring.replace('#', '').replace('\t', '').replace(' ', '').replace(',', '\n')
wordlist = wordstring.split('\n')
newWord = ''
while x != 10:
    try:
        letters = list(wordlist[x])
    except IndexError:
        break    
    letters.sort()
    abcString = str(letters).strip(" [] ").replace(',', '').replace("\'", '').replace(' ', '')
    newWord += abcString + '\n'
    x += 1
abcWordList = newWord.split('\n')
x = 0
Solution = ''
while x != 10:
    y = 0
    while y != 1275:
        if abcWordList[x] == abcDictList[y]:
            break
        else:
            y += 1
    Solution += dictlist[y] + ','
    x += 1
solutionWrite = open('solution.txt', 'w')
solutionWrite.write(Solution)


Somehow your code lost all its indentations, I had to guess them back in manually, hope they are okay. Give it a try!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Thanks for the reply and explanation! It gives me a better understanding!

defience
Newbie Poster
17 posts since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

Are you trying to unscramble words?

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Yes, the program is for unscrambling 10 words at a time and printing them out on a single line like:word1,word2,word3, etc.

defience
Newbie Poster
17 posts since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

You might want to try

import string
file_pointer= open('wordlist.txt', 'U') 
dictList = file_pointer.readlines()
file_pointer.close() 
 
x = 0
newDict = ''
stop = len(dictList)
while x < stop:
##  you could also use "for each_rec in dictList :"
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Thanks woooee, I actually ended up writing my own code, which was a lot simpler (at least to me:))

defience
Newbie Poster
17 posts since Jan 2007
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You