34. Mini Projects

The chapters in this book mostly teach one concept at a time. Mini-projects are where you combine several ideas into a single usable program.

digraph projects { rankdir=LR; node [shape=box, style="rounded"]; input [label="input"]; logic [label="logic"]; files [label="files / config"]; output [label="output"]; input -> logic -> output; files -> logic; }

34.1. Project 1: Personal Expense Tracker

Build a command-line program that:

  • reads expenses from a CSV file

  • groups them by category

  • prints totals and the largest category

  • optionally writes a JSON summary file

34.2. Project 2: File Organizer

Build a script that scans a folder and sorts files into subfolders such as images, notes, and data based on file extension.

34.3. Project 3: Quiz Game

Build a small terminal quiz that:

  • loads questions from JSON

  • shuffles the question order

  • tracks correct answers

  • prints the final score

34.4. One complete solution

As a concrete example, here is a small solution for the quiz-game project shape.

 1import json
 2import random
 3
 4
 5QUESTIONS = json.loads(
 6    """
 7    [
 8      {"question": "2 + 2 = ?", "answer": "4"},
 9      {"question": "Capital of France?", "answer": "paris"},
10      {"question": "Python file extension?", "answer": ".py"}
11    ]
12    """
13)
14
15random.shuffle(QUESTIONS)
16score = 0
17
18for item in QUESTIONS:
19    print(item['question'])
20    guess = input('> ').strip().lower()
21    if guess == item['answer']:
22        score += 1
23        print('correct')
24    else:
25        print(f"wrong, expected {item['answer']}")
26
27print(f'score: {score}/{len(QUESTIONS)}')