[Tutor] trouble getting a list to update
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Fri Jan 7 01:42:45 CET 2005
On Thu, 6 Jan 2005, Vincent Wan wrote:
> On Jan 6, 2005, at 12:59 PM, Danny Yoo wrote:
> > Can you show us a snippet of the file output? I'm not immediately
> > seeing anything particular with your debugging output statements:
>
> > Like the computer, I don't yet understand what the problem is. *grin*
>
> > If you can point us at the output that looks weird to you, and tell us
> > what you expected to see instead and why, that will help us a lot to
> > better understand the situation.
Hi Vincent,
Ok, now I understand and see the bug. write_list() contains the problem:
###
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]) + ' ')
file_name.write('\n')
###
The bug is here:
file_name.write(str(list_to_write[each]) + ' ')
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.
You probably meant to write:
file_name.write(str(each) + ' ')
Your list was mutating just fine: the output function simply was obscuring
what was happening.
Best of wishes to you!
More information about the Tutor
mailing list