Pure functions, effects at the edges
A pure function returns a result that depends only on its arguments, and changes nothing else: it does not print, write files, update globals or modify its inputs. Pure functions are easy to test, because a test is just a call and a comparison, and they are safe to reuse.
Programs still need effects. Keep them at the edges: a thin outer function reads the input, hands it to the pure core, and writes out the result.
def count_words(lines):
return sum(len(line.split())
for line in lines)
def main(path):
with open(path) as file:
lines = file.read().splitlines()
print(count_words(lines), "words")
with open("notes.txt", "w") as file:
file.write("one two\nthree\n")
main("notes.txt")count_words can be tested with a list and no file at all, and main is so thin it hardly needs a test.
Your task
Write a pure order_price(order) and a pure summarize(orders) returning (lines, total), with lines like "order 1: 20". Make print_summary(orders) the only function that prints: each line, then total: 25. Use no globals and do not change the orders.
The explanation opens here once every check passes.