AzLearn

تمرين إعادة البناء

حاسب إجمالي الفاتورة

احسب إجماليات فاتورة من CSV باستخدام الهللات أو السنتات كأعداد صحيحة.

python ~25 دقيقة متوسط
أعد بناء الكود Rebuild

هذا هو الكود. اكتبه بنفسك.

الكود المرجعي
import argparse
import csv
from pathlib import Path


def money(cents: int) -> str:
    return f"{cents // 100}.{cents % 100:02}"


def total_invoice(path: Path, tax_basis_points: int) -> None:
    subtotal = 0
    with path.open(newline="", encoding="utf-8") as file:
        for row in csv.DictReader(file):
            quantity = int(row["quantity"])
            unit_cents = int(row["unit_cents"])
            line_total = quantity * unit_cents
            subtotal += line_total
            print(f"{row['item']}: {money(line_total)}")
    tax = subtotal * tax_basis_points // 10_000
    print(f"Subtotal: {money(subtotal)}")
    print(f"Tax: {money(tax)}")
    print(f"Total: {money(subtotal + tax)}")


def main() -> None:
    parser = argparse.ArgumentParser(description="Total invoice rows from CSV integer cents.")
    parser.add_argument("csv_file", type=Path)
    parser.add_argument("--tax-bps", type=int, default=1500)
    args = parser.parse_args()
    total_invoice(args.csv_file, args.tax_bps)


if __name__ == "__main__":
    main()
اكتب هنا