473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with a for loop and a list


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
5 1378
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Ling Lee | last post by:
Hello. I'm trying to write a small program that lets you put in a number as an integer and then it tells you the textuel representation of the number. Like if your input is 42, it will say...
17
by: Thomas M. | last post by:
Hello, i have a question on the following code. test_list = for i in test_list: print i
12
by: teoryn | last post by:
I've been spending today learning python and as an exercise I've ported a program I wrote in java that unscrambles a word. Before describing the problem, here's the code: *--beginning of file--*...
27
by: John Salerno | last post by:
Ok, here's a problem I've sort of assigned to myself for fun, but it's turning out to be quite a pain to wrap my mind around. It's from a puzzle game. It will help if you look at this image: ...
18
by: Daniel | last post by:
Hey guys I have an instance of an object say: List<Object> myList = new List<Object>(); Object myObject = new Object(); myObject.PositionVector = new Vector3(10,10,10); ...
8
by: Kappadon5 | last post by:
Hello All, I am new to programming and I apologize in advance if I am out of protocol in any way shape or fashion. My problem is that I have a program where you select an option from two different...
1
by: Promextheus Xex | last post by:
First php post... Hi there, I'm writing some code for my website to list people listening to my music. I have a loop for an array that dosen't seem to display all people. most of the time it...
3
by: Peterwkc | last post by:
Hello all expert C++ programmer, i fairly new to C++ programming. I have a class cellphone which contain dynamic pointer. I have create (example)ten cellphone. I want to ask user for the...
0
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
7
by: Dave | last post by:
Hey there, having a bit of problem iterating through lists before i go on any further, here is a snip of the script. -- d = "a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5 c5...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.