Page 153 -
P. 153
getting the highest score
Code Magnets Solution
You needed to complete the code to find the highest score in the
results.txt file. Remember: the for loop creates a string from
each line in the file.
Hint: For the program to work, you needed to convert the string into
a number.
highest_score = 0 The “highest_score" variable gets
contains a higher score.
result_f = open("results.txt") updated every time you find a line that
for line in result_f:
Remember to if ( ) :
>
line
float
highest_score
indent the code
inside the loop AND = ( )
highest_score
line
float
again inside the
“if"statement. result_f.close()
Remember to convert the string to
print("The highest score was:") a number with float(). Even though
the line is a number, it comes into
print(highest_score) the program as a string.
After the loop runs, the “highest_score"
variable should have the best score from the
data file, so you can then go ahead and display
it on screen.
Load this!
To successfully run this program, you need to grab a copy of the results.txt
data file from the Head First Programming website. Be sure to put the data file in
the same directory (or folder) that contains your code.
118 Chapter 4