February 2022
Intermediate to advanced
274 pages
6h 28m
English
Testing scripts can be quite fun. Running through the process on a second script will help you remember the techniques in this chapter.
The exercises start with an example script, sums.py, that adds up numbers in a separate file, data.txt.
Here’s sums.py:
| | # sums.py |
| | # add the numbers in `data.txt` |
| | |
| | sum = 0.0 |
| | |
| | with open("data.txt", "r") as file: |
| | for line in file: |
| | number = float(line) |
| | sum += number |
| | |
| | print(f"{sum:.2f}") |
And here’s an example data file:
| | 123.45 |
| | 76.55 |
If we run it, we should get 200.00:
| | $ cd /path/to/code/exercises/ch12 |
| | $ python sums.py data.txt |
| | 200.00 |
Assuming valid numbers in data.txt, we need to test this script.
Write ...
Read now
Unlock full access