[Tutor] trouble getting a list to update
Alan Gauld
alan.gauld at freenet.co.uk
Fri Jan 7 09:34:00 CET 2005
> 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]) + ' ')
This is the problem.(I think)
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!
Also you claim to be writing to a file_name, that
needs to be an open file object...
You need:
for each in list_to_write:
file_object.write(str(each) + ' ')
However its much easier still to just write the list in
one go:
file_object.write(str(list_to_write) + '\n')
> ...snipped code here...
> myfile = open(....)
Ah, OK you have actually opened the file, I thought you must
since otherwise you;'d get no output. BUt I think you should
change the variable name above...
HTH,
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list