{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Sage script to illustrate loan payment computations for \"Money Matters\" project in Chapter 2.\n",
    "\n",
    "#First choose interest rate \"r\", initial borrowed amount \"p0\", monthly paymnet \"b\", and number of payments \"payments\".\n",
    "\n",
    "r = 0.03 #Annual interest rate\n",
    "b = 1726.45 #Monthly payment\n",
    "payments = 180 #Number of payments\n",
    "p = [250000] #Initial payment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Utility function to round to nearest cent, for aesthetics\n",
    "r2 = lambda x: round(100*x)/100.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Loop over months, store balance in list \"p\":\n",
    "print(\"Month\", 0, \"Balance\", p[0])\n",
    "for i in range(1,payments+1):\n",
    "    pnext = (1+r/12)*p[i-1] - b #Next monthly balance\n",
    "    p.append(pnext) #Append it to the list p.\n",
    "    print(\"Month\", i, \"Interest\", r2(r/12*p[i-1]), \"Balance\", r2(pnext))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 9.2",
   "language": "sage",
   "name": "sagemath"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}