Page 169 -
P. 169
high scores in the array
You were to rewrite the program so that each time you read a
new score you append it to the end of an array called scores.
You were then to amend the code to print out the first 3 scores in
the list.
Here's the code as highest_score = 0
it currently stands. result_f = open("results.txt")
for line in result_f:
(name, score) = line.split()
if float(score) > highest_score:
highest_score = float(score)
result_f.close()
print("The highest score was:")
print(highest_score)
Start with an
empty array.
scores = []
result_f = open(“results.txt")
Process the data in
the file as before... for line in result_f:
(name, score) = line.split()
...but, this time, append the scores.append(float(score))
scores to an array.
result_f.close()
print(“The top scores were:")
print(scores[0])
With the data safely print(scores[1])
stored in the array,
print out the first 3 print(scores[2])
array elements.
134 Chapter 4