Basics · lesson 10 of 10
Parts in this lesson
A[(title, days)]1.9: a list of tuples
Bdef fine_for1.8: one job, one function
Cif fine > MAX_FINE1.6: the limit
Dtotal +=1.7: a running total
E{x:<10} {x:.2f}columns and money

Project: overdue fines

Last updated

Time to put Stage 1 together. You'll build the first real piece of the library system: a program that works out the fine for each late book, with a limit on how much one book can cost, adds them up and prints a neat receipt.

By the end of this lesson you can

  • Plan a small program before writing it
  • Combine lists, loops, if, functions and f-strings in one program
  • Line up columns and show money with f-string formats like :>7 and :.2f
  • Use names in capitals for values that never change

Plan it

Every program starts with a plan, written in plain words before any code. Here's this one.

The library's rules: a late book costs 25 cents for each day it's late. No book costs more than 500 cents (5.00), however late it is. A book returned on time costs nothing.

What the program does: it takes the books returned today, each with its title and how many days late it is. It works out each book's fine, adds them up, and prints this receipt:

OVERDUE FINES
--------------------------
Dune         3 days   0.75
Emma         0 days   0.00
Beloved     12 days   3.00
Matilda     30 days   5.00
--------------------------
Total                 8.75

The pieces, and the lesson each comes from:

  1. A list of (title, days) tuples for the returns (lesson 1.9).
  2. A function, fine_for(days_late), that returns one book's fine, using if for the limit (1.8 and 1.6).
  3. A loop with a running total (1.7).
  4. f-strings to print it all in neat columns (1.2, plus one new thing in step 5).

Money is counted in whole cents, never as decimals like 0.25. Lesson 1.4 showed why: decimals aren't stored exactly, and that's not what you want for money. The program only turns cents into 8.75 at the very end, to print it.

The project builds these pieces one step at a time. Each step's code is complete, so you can run it.

The days late, in a list

Start with the data and a loop that prints it, to check it's all there. For now each book is just its number of days late. The titles come back in step 6.

Step 2 code main.py
Edit the code, then run it again

One fine: a function with a limit

The rules from step 1 become a function. Each rule is one or two lines, in the same order as the plan:

  • On time or early (days_late <= 0): return 0 straight away.
  • Otherwise the fine is days times 25.
  • If that's over the limit, use the limit instead.

The 25 and the 500 are given names in capitals, FINE_PER_DAY and MAX_FINE. As lesson 1.3 said, capitals mean "this value never changes". The names also say what the numbers mean, and if the library changes its rules, there's one place to change them.

Step 3 code main.py
Edit the code, then run it again

Check each line of output against the rules by hand. 12 days is 300 cents, under the limit. 30 days would be 750 cents, so the limit makes it 500.

Add them up

The loop now keeps a running total instead of printing each fine, the same pattern as lesson 1.7. fine_for doesn't need to change at all: that's the point of putting one job in one function.

Step 4 code main.py
Edit the code, then run it again

Check it: 75 + 0 + 300 + 500 is 875 cents.

Columns and money in f-strings

Inside an f-string's braces, a colon after the value starts a format: how to show the value.

  • :<10 pads the value to 10 characters, lined up on the left. :>4 pads it to 4, lined up on the right. Numbers usually line up on the right, so their last digits sit in a column.
  • :.2f shows a number with exactly 2 decimal places. 875 / 100 is 8.75, and 0 / 100 is 0.0, which :.2f shows as 0.00.

The square brackets in the output show where the padding is.

Step 5 code main.py
Edit the code, then run it again

Change :<10 to :>10 and :.2f to :.1f, and run it again to see what each part does.

The finished receipt

Here's the whole program. The returns are now (title, days) tuples, unpacked in the for line as in lesson 1.9. A small function, money, turns cents into text like 8.75, and every line of the receipt uses the formats from step 5. Run it, then Watch it run and follow one book from the list to the receipt.

Step 6 code main.py
Edit the code, then run it again

Some things to try:

  • Add a fifth book to returns, such as ("Persuasion", 8). The total updates by itself.
  • Change the rules: make MAX_FINE 400. Only one line changes.
  • Give the receipt a date line at the top, using input() to ask for it (lesson 1.5).
A book comes back 25 days late. What is its fine, in cents?

That's Stage 1 done: you can now write programs that calculate, decide and repeat. In Stage 2 this receipt grows into a catalogue you can search, read from a file and save.

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python