Alright, so i got this question that im given a group of scores in the file, and i have to make them into a list, sort the list, and find the median. I got the sorting and the median done..

But I just got one problem that I can't seem to figure out. Here is my code:
I'm new to python..i know i can use sort() and everything..but is there a way to solve the problem with the code i used?:)

``````#Text file in another file:
score.txt
10,11,09,10,22,22,20,01``````
``````def elements(score_file):
score = line.split(",")

while line != "":
score = score + line.split(",")

print("Original list:")
print(score)
return score``````
``````#The ouput i get for the orginal list comes up to(How do i get rid of the '' at the end?):
Original list:
['10', '11', '09', '10', '22', '22', '20', '01', '']``````

THANKS ALOT GUYS!!

Edited by dragonstear: n/a

3
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by dragonstear

All you need to do is call the sort() and pop() methods.

I know, but lets assume that its not allowed:(

All you need to do is call the sort() and pop() methods.

You'll need to write custom functions. If list comprehensions are allowed you could easily iterate through to all but the last item, then write a bubblesort or quicksort. If not comprehensions you're missing a key factor to Python.

I got the sorting and the median done..

I do not see any sorting and median, so I am confused of what you want.

``````def elements(score_file):
return [int(score)
for score_line in open(score_file)
for score in score_line.split(",")]

print sorted(elements('score.txt'))``````

Edited by pyTony: n/a

thats in two other functions. not really important
but i still can't get rid of the blank '' at the end of the list

I do not see any sorting and median, so I am confused of what you want.

``````def elements(score_file):
return [int(score)
for score_line in open(score_file)
for score in score_line.split(",")]

print sorted(elements('score.txt'))``````

Edited by dragonstear: n/a

``````def elements1(score_file):
return [int(score)
for score_line in open(score_file)
for score in score_line.split(",")]

#cleaner
def elements2(score_file):
score = []
for line in score_file:
score = score + line.rstrip().split(",")
return score

#minimal change
def elements(score_file):
score = line.split(",")

while line != "":
if line: score = score + line.split(",")

return score

print(sorted(elements1('score.txt')))
print(elements2(open('score.txt')))
print(elements(open('score.txt')))``````

Edited by pyTony: n/a

thanks alot!

Thank you so much!!!

``````def elements1(score_file):
return [int(score)
for score_line in open(score_file)
for score in score_line.split(",")]

#cleaner
def elements2(score_file):
score = []
for line in score_file:
score = score + line.rstrip().split(",")
return score

#minimal change
def elements(score_file):