PROBLEM SOLVED THANK YOU DANNY AND ALAN

To recap:

My program main loop called two functions one that changed a list and one that printed the list. Turns out that print function was bad.

I didn't understand how clever pythons for statement is. I wrote:

def write_list(list_to_write, file_name):
     "Writes elements of a list, seperated by spaces, to a file"
     for each in list_to_write:
         file_name.write(str(list_to_write[each]) + ' ')

The bug is here:

         file_name.write(str(list_to_write[each]) + ' ')

should have been

      file_name.write(str(each) + ' ')

Danny points out

Do you see why this is buggy? 'each' is not an index into list_to_write,
but is itself an element of list_to_write.


and, Alan explains

You are taking each element of the list then using
it(either 0 or 1) as an index into the list, which
means you will only ever print the first two elements!


So

for x in list:
        do_something_with_an_element(list[x])

and python keeps track of x for you.


Vincent

------------------------------------------------------------------------ --------------
PhD Candidate
Committee on the Conceptual and Historical Studies of Science
University of Chicago


PO Box 73727
Fairbanks, AK 99707

[EMAIL PROTECTED]

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to