Keep the Best Score

Python for AIMedium+10 XP+10

Keep the Best Score

A game keeps its high score in a file. The file holds one whole number followed by a newline, like "40\n".

Write update_best(path, score):

  1. Read the number that's in the file now.
  2. If score is higher, replace the file's contents with score followed by \n.
  3. Otherwise leave the file exactly as it is.
  4. Read the file again and return the number in it, as an int.

A tie is not higher, so the file stays the same. Leave the make_file helper as it is.

Examples

path = make_file('best.txt', '40\n')
score = 75
returns 75
path = make_file("ub_a.txt", "100\n")
score = 250
returns 250
path = make_file("ub_b.txt", "100\n")
score = 30
returns 100

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run