Sign InCoursewareNuggetsTutorials AboutClassesCodePad

Score Ranking

After we have our game scores saved in a file, we may display the score ranking at the end of a game. In the following code segment, file "score.txt" is opened in read mode as score_file. Then the file content is read using method readlines( ) that returns a list with each line as an element. The list is saved in a variable named score_record.

with open(os.path.join(dir, "score.txt"), "r") as score_file: score_record = score_file.readlines()

For example, if "score.txt" has four scores saved in four lines, score_record will have four elements, one element for each line.


score_record = [225\n, 195\n, 270\n, 930\n]
© CS Wonders·About·Classes·Cheatsheet