…
…
Read a campus library's loan records from a CSV file; calculate late days and fees with dates, count the most borrowed books, and write the member report to a new CSV file.
Maya, a volunteer at the campus library, put the fall term's loan book into the file loans.csv: 140 loans, each with a book, a member number, a borrow date, and (if the book came back) a return date. The rule is simple: a book is returned within 15 days, every day late adds a $0.25 fee, and the fee for one loan never goes above $5.00. In this project you will write the program that produces Maya's end-of-term report.
What you will build: A reminder list of books that are late and still out, the five most borrowed books, and a CSV file (member_report.csv) with a summary for every member:
15 loans will get a reminder
L-001 M103 Night Train 64 days $5.00
...
Top 5 most borrowed books:
1. Notes on Algorithms 15
...
18 members written to the file
{'member': 'M101', 'loans': '2', 'late': '0', 'fee': '0.00'}Lessons used: functions (14-16), formatting (22), lists and sorting (23-24), dictionaries and lists of records (26-28), comprehensions (30), sorting with lambda (31), CSV files (34), datetime, timedelta, and Counter (36-37).
How to work: The cells share one session: the records list you read in the first step gets new keys in each step (borrowed_d, days_late, fee ...) and the later steps use them, so run the steps in order. So the report gives the same result every time, "today" is fixed as November 20, 2026. Check also tries your functions on other examples besides the records in the file.
What you'll do
loans.csvDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Maya, a volunteer at the campus library, put the fall term's loan book into the file loans.csv. Each row is one loan: loan number, book title, member number, borrow date, and return date. If the book has not come back yet, the returned column is empty.
Read the file with csv.DictReader and collect the rows in a list: each row becomes a dictionary that uses the column names as keys. Remember to pass encoding="utf-8" and newline="" when you open a CSV file. The reader gives every value as a string; you will convert the dates in the next step.
This step's code cell is for members.Sign up free
"2026-09-02" is a string; you cannot subtract two strings to find how many days passed. First you need to convert it to a date: datetime.strptime(text, "%Y-%m-%d").
The function parse_date(text) should return None if the string is empty, and the date otherwise. Then add two new keys to every record:
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
"borrowed_d""returned_d"returned_dNoneThis step's code cell is for members.Sign up free
The library rule: a book must be returned within 15 days. The due date is the borrow date + 15 days; a book returned on that day is not counted as late.
TODAY = datetime(2026, 11, 20).The difference of two dates is a timedelta; .days gives the number of days.
This step's code cell is for members.Sign up free
For each day late, a fee of $0.25 is charged, but the fee for one loan cannot go above $5.00 (the cap). A member who is 25 days late pays $5.00, not $6.25.
Write the function fee_for(days), then add a "fee" key to every record and build up the total fee. Also count the loans that reached the cap: Maya will report those separately to the library director.
This step's code cell is for members.Sign up free
Members who still have books out past their due date will get a reminder email. The longest-overdue ones should be at the top.
First select these records (not returned and more than 0 days late), then sort them by lateness from largest to smallest with sorted. You give the sort key with key=lambda r: r["days_late"], and reverse=True reverses the order. The report lines are ready; they print the first 5 records aligned.
This step's code cell is for members.Sign up free
The library wants to add more copies for the new term; which books were borrowed most? Counter from the collections module is made for this "count and sort" job: it counts the title of every record, and most_common(5) gives the five most frequent titles as (title, count) pairs.
Print the list with a rank number, the title left-aligned to 26 characters, and the count right-aligned to 3 characters.
This step's code cell is for members.Sign up free
Maya wants a term summary for every member: how many books they borrowed, how many they returned late, and how much fee has built up in total. Use a dictionary inside a dictionary for this: member_report["M103"] → {"loans": 9, "late": 4, "fee": 18.0}.
While you go through the records, if the member is not in the dictionary yet, add them with zeros first, then update the numbers. Then sort the members who have a fee from largest fee to smallest and print the top 5. member_report.items() gives a (number, summary) pair for each member; the sort key is the fee in the second element of the pair: lambda x: x[1]["fee"].
This step's code cell is for members.Sign up free
The last job is to write the member report to a file that Maya can open in her spreadsheet program. csv.DictWriter writes dictionaries: you give the column names with fieldnames, writeheader() writes the header, and each writerow(dictionary) writes one row.
Let the rows be in member number order (sorted(member_report) sorts the keys). The dictionary for each row must carry the member number and the three numbers from the summary. Write the fee with two decimals: f"{summary['fee']:.2f}". After you write the file, the ready-made lines read it back to check it.
This step's code cell is for members.Sign up free