By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,688 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,688 IT Pros & Developers. It's quick & easy.

Problem with a for loop and a list

P: n/a

I am not sure what is going on here. Here is the code that is being run:

def getWords(self):
self.n=0
for entry in self.listBuffer:
self.wordList[self.n] = entry.get()
self.n=self.n+1

print self.wordList

This is the "listBuffer" that you see:

self.listBuffer=[self.e1, self.e2, self.e3, self.e4,
self.e5, self.e6, self.e7, self.e8,
self.e9, self.e10, self.e11,
self.e12, self.e13, self.e14]

(side note, those are all tkinter entry widgets, and the get() function gets
the text)

this is the error the interpreter is giving me when I run it:

self.getWords()
File "C:/Documents and Settings/Alex/My Documents/PYTHON/DictionaryApp/The
GUI.py", line 153, in getWords
self.wordList[self.n] = entry.get()
IndexError: list assignment index out of range

I have no idea what "list assignment index out of range means?!?!

--
View this message in context: http://www.nabble.com/Problem-with-a...p18232298.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jul 2 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a
On 2008-07-02, Alexnb <al********@gmail.comwrote:
I have no idea what "list assignment index out of range means?!?!
You are assigning a value to a non-existing list element, as in
>>x = [1]
x[2] = 4
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: list assignment index out of range
Albert
Jul 2 '08 #2

P: n/a

well okay, so what can I do?

A.T.Hofkamp-3 wrote:
>
On 2008-07-02, Alexnb <al********@gmail.comwrote:
>I have no idea what "list assignment index out of range means?!?!

You are assigning a value to a non-existing list element, as in
>>>x = [1]
x[2] = 4
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: list assignment index out of range
Albert
--
http://mail.python.org/mailman/listinfo/python-list

--
View this message in context: http://www.nabble.com/Problem-with-a...p18232528.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jul 2 '08 #3

P: n/a
Alexnb wrote:
>
I am not sure what is going on here. Here is the code that is being run:

def getWords(self):
self.n=0
The value of self.n only makes sense within the method, so you better use
the local variable n instead of the instance attribute self.n
for entry in self.listBuffer:
self.wordList[self.n] = entry.get()
self.n=self.n+1

print self.wordList

This is the "listBuffer" that you see:

self.listBuffer=[self.e1, self.e2, self.e3, self.e4,
self.e5, self.e6, self.e7, self.e8,
self.e9, self.e10, self.e11,
self.e12, self.e13, self.e14]

(side note, those are all tkinter entry widgets, and the get() function
gets the text)

this is the error the interpreter is giving me when I run it:

self.getWords()
File "C:/Documents and Settings/Alex/My
Documents/PYTHON/DictionaryApp/The
GUI.py", line 153, in getWords
self.wordList[self.n] = entry.get()
IndexError: list assignment index out of range

I have no idea what "list assignment index out of range means?!?!
As Albert explained this means that wordList is shorter than listBuffer. The
most flexible remedy is to have wordList grow dynamically:

def getWords(self):
wordList = []
for entry in self.listBuffer:
wordList.append(entry.get())
print wordList

If for some reason you want to remember the values in wordList add

self.wordList = wordList

to the method (this is the same consideration as for self.n versus n).

Peter

Jul 2 '08 #4

P: n/a
Alexnb wrote:
well okay, so what can I do?
Firstly, stop top posting. Replies to a thread "flow" better if bottom
posted.

Secondly, it sounds like you want to build a list of the results from
your entry.get() calls.

Try this:

self.wordList = []
def getWords(self):
for entry in self.listBuffer:
self.wordList.append(entry.get())

print self.wordList
Jul 2 '08 #5

P: n/a
Alexnb a écrit :
I am not sure what is going on here. Here is the code that is being run:

def getWords(self):
self.n=0
for entry in self.listBuffer:
self.wordList[self.n] = entry.get()
self.n=self.n+1
print self.wordList

def get_words(self):
self.word_list = [e.get() for e in self.list_buffer]

This is the "listBuffer" that you see:

self.listBuffer=[self.e1, self.e2, self.e3, self.e4,
self.e5, self.e6, self.e7, self.e8,
self.e9, self.e10, self.e11,
self.e12, self.e13, self.e14]
What a fantastic naming scheme. So easy to read, understand and
maintain. Congratulation, you just won the right to read "How to write
unmaintainable code":

http://mindprod.com/jgloss/unmain.html
Jul 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.