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 <module>
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)

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.

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.

So your file has one word per line?

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

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.

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!

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

Are you trying to unscramble words?

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

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 :"

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.