I have a .txt file with the following lines:
pablo 9.50
sergio 2
Rose 10
oto 11.4
maria 7.9
and I have the following program:
scores = {}
read = open("C:/Users/renato/Desktop/HTML Files/myfile.txt")
for cont in read:
(name,score)=cont.split()
scores[score] = name
read.close()
print("The top scores are: ")
for eachscore in sorted(scores.keys(), reverse = True):
print("Surfer "+scores[eachscore]+" scored "+eachscore)
When I run the program, it returns the same list, just as seen on the file.
I'm trying to sort the results, hence I used the sorted() function to sort the keys of the 'scores' dictionary. But the entries are being printed in the same order, not sorted as expected.
Am I'm missing something here?
Thanks!
scores[name] = float(score)in line 5 of your codeint()truncates the decimal point.print("Surfer "+eachscore+" scored "+scores[eachscore])