Hello to all, haven't been on here in a while but recently been having major problems with trying to finish this second problem set my teacher has given me and the other students for the gr. 12 ics college coarse, currently im gr. 11 but this coarse had to be a filler for my math being switched to 2nd period. any way, the problem is that we have just started to do file opening, reading, writing, and other stuff related to files, my problem is that im on the second question now and were supposed to make a file before hand for this program called "studentAges.txt", that have student ages in random order (I have it from 12 to 20, like so: 12, 20, 19, 17, 14, 13, 15, 18, 16), and get the program to read those ages in, put them into a list, sort them into ascending order, then append the list and put them into an output file.
here is my code:
file_in=open("studentAges.txt", "r")
file_out=open("StudentAges(Sorted).txt", "w")
ages=file_in.readline()
list1=[]
for x in range (0,11):
ages=file_in.readline()
list1.append(ages)
list1.sort(cmp=None, key=None, reverse=False)
list1.append(ages)
print list1
file_out.write(str(list1))
file_in.close()
file_out.close()
for me though, i think my problem just rests in this part of the code:
for x in range (0,11):
ages=file_in.readline()
list1.append(ages)
list1.sort(cmp=None, key=None, reverse=False)
list1.append(ages)
print list1
i just think there is either something missing or that I'm doing wrong
and this is the end result that i get in the idle shell when after I've told my program to print list1 to show me what went into my output file:
['', '', '', '', '', '13\n', '13\n', '14\n', '14\n', '15\n', '15\n', '16', '16', '17\n', '17\n', '18\n', '18\n', '19\n', '19\n', '20\n', '20\n', '']
It's almost right but the 12 is missing, every thing is being repeated 2 times, and all of them except for 16 has the new line character symbol next to it.
any and all help is appreciated, but i dont want to just be given the answer, im still in school trying to learn =).
thanks in advance,
lucas (my real name..)