Add Up the File

Python for AIMedium+10 XP+10

Add Up the File

A step counter saves each day's steps as a whole number on its own line. Some days were skipped, which left blank lines.

Write total_steps(path). Return the sum of all the numbers in the file.

  • Skip any line that is empty once stripped (this includes lines with only spaces).
  • Numbers may have spaces around them.
  • Numbers can be negative (a correction).
  • An empty file gives 0.

Leave the make_file helper as it is.

Examples

path = make_file('steps.txt', '4000\n\n5500\n')
returns 9500
path = make_file("ts_a.txt", "100\n200\n300\n")
returns 600
path = make_file("ts_b.txt", " 50 \n\n \n25\n")
returns 75

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