{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercices du livre - Chapitre 7 - solutions\n",
    "\n",
    "---\n",
    "\n",
    "<table class=\"appbclp1\">\n",
    "<tr> <td style=\"font-weight: bold!;\">auteurs :</td><td style=\"text-align: left!;\">Laurent Pointal &lt;<a href=\"mailtp:laurent.pointal@limsi.fr\">laurent.pointal@limsi.fr</a>&gt; &amp; Bob Cordeau &lt;<a href=\"pycours@kordeo.eu\">pycours@kordeo.eu</a>&gt; </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">licence :</td><td style=\"text-align: left!;\">Creative Commons Attribution-ShareAlike 4.0 International Public License (CC BY-SA 4.0) </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">source :</td><td style=\"text-align: left!;\"><a href=\"https://github.com/lpointal/appbclp\">https://github.com/lpointal/appbclp</a></td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">description :</td><td style=\"text-align: left!;\">Les programmes dans ce Notebook Jupyter servent de support au livre <span style=\"font-style: italic!;\">« Python 3 - Apprendre à  programmer dans l'écosystème Python»</span>, édition Dunod, dans lequel les différentes sections sont expliquées </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">url :</td><td style=\"text-align: left!;\"><a href=\"https://www.dunod.com/sciences-techniques/python-3\">https://www.dunod.com/EAN/9782100809141</a></td> </tr>\n",
    "</table>\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Exercice 1\n",
    "\n",
    "lst = [('Arsenic', 17.8464, 3.12), ('Aluminium', 16.767, 6.21), ('Or', 239320, 12400)]\n",
    "\n",
    "with open(\"../../data/elements.txt\", \"w\", encoding=\"utf-8\") as f:\n",
    "    for nom, masse, volume in lst:\n",
    "        mvol = masse / volume\n",
    "        s = f\"{nom} = {mvol:.2f} g/cm3\\n\"\n",
    "        f.write(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Exercice 2\n",
    "\n",
    "volume = float(input(\"Volume de matière (cm3) : \"))\n",
    "\n",
    "with open(\"../../data/elements.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    for ligne in f:\n",
    "        items = ligne.split(\"=\")\n",
    "        nom = items[0].strip()\n",
    "        mvol = float(items[1].split()[0])\n",
    "        masse = volume * mvol\n",
    "        print(f\"\\n{nom:9} => {masse:.2f} g\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Exercice 3\n",
    "\n",
    "from time import asctime\n",
    "def note_journal(nomfichier, message):\n",
    "    \"\"\"Stockage d'un message horodaté.\"\"\"\n",
    "    f = open(nomfichier, \"a\", encoding=\"utf-8\")\n",
    "    f.write(asctime())\n",
    "    f.write(f\" {message}\\n\")\n",
    "    f.close()\n",
    "    \n",
    "# Tests\n",
    "import time\n",
    "for i in range(10):\n",
    "    note_journal(\"jourheure.txt\", f\"Un message {i+1}.\")\n",
    "    print('*', end='')\n",
    "    time.sleep(1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
