{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapitre 3 : \"Contrôle du flux d’instructions\" - 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": [
    "### exo03_01\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_01\n",
    "\n",
    "ch = input(\"Entrez une chaîne de caractères : \")\n",
    "cr = 'e'        # caractère à rechercher\n",
    "\n",
    "# recherche :\n",
    "lc = len(ch)    # longueur de la chaine\n",
    "i = 0           # indice de la boucle while\n",
    "nb = 0          # initialisation du nombre d'occurrences de cr\n",
    "trouve = False  # passera à vrai si cr est dans ch\n",
    "\n",
    "while i < lc:\n",
    "    if ch[i] == cr:\n",
    "        trouve = True\n",
    "        nb = nb + 1\n",
    "    i = i + 1\n",
    "    \n",
    "# affichage - on construit la ligne affichée en évitant les retours\n",
    "# à la ligne grâce à l'option end='' de print\n",
    "print(\"\\nLe caractère 'e'\", end='')\n",
    "if trouve:\n",
    "    print(f\" est present {nb} fois\", end='')\n",
    "else:\n",
    "    print(\"est introuvable\", end='')\n",
    "print(f\" dans la chaine '{ch}'\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_02\n",
    "\n",
    "ch = input(\"Entrez une chaîne de caractères : \")\n",
    "cr = \"*\"      # caractère à insérer\n",
    "lc = len(ch)\n",
    "i = 1         # on part du deuxième caractère, d'index 1\n",
    "nch = ch[0]   # nouvelle chaine, on reprend le premier caractère de 'ch'\n",
    "\n",
    "while i < lc:\n",
    "    nch = nch + cr + ch[i]\n",
    "    i = i + 1\n",
    "\n",
    "print('\\n', nch)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_03"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_03\n",
    "\n",
    "ch = input(\"Entrez une chaîne de caractères : \")\n",
    "nch = \"\"      # nouvelle chaîne à construire\n",
    "\n",
    "# boucle en parcourant les index de la fin au début de ch\n",
    "i = len(ch) - 1\n",
    "while i >= 0:\n",
    "    nch = nch + ch[i]\n",
    "    i = i - 1\n",
    "\n",
    "print(f\"\\nChaîne initiale : '{ch}', chaîne inversée : '{nch}'.\")\n",
    "if nch == ch:\n",
    "    print(\"C'est un palindrome.\")\n",
    "else:\n",
    "    print(\"Ce n'est pas un palindrome.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_04"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_04\n",
    "\n",
    "saisie = input(\"Saisissez une phrase : \")\n",
    "phrase = saisie.split()   # découpe en liste de \"mots\" à partir des espaces\n",
    "\n",
    "l_max = 0\n",
    "for mot in phrase:\n",
    "    if len(mot) > l_max:\n",
    "        l_max = len(mot)  # devient la plus grande longueur courante\n",
    "        mot_max = mot     # le plus long mot courant\n",
    "        \n",
    "print(f\"\\nPlus long mot de la phrase : '{mot_max}'\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_05"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_05\n",
    "\n",
    "a, b, c = 1, 1, 1\n",
    "while c < 9:\n",
    "    #print(b, end=\" \")\n",
    "    # utilisation du séparateur tabulation pour aligner les valeurs\n",
    "    print(a, b, c, sep='\\t')\n",
    "    a, b, c = b, a+b, c+1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On observe dans la dernière colonne l'incrément de la variable `c` qui contrôle simplement le nombre d'itérations dans la boucle, et dans la seconde colonne l'évolution de la variable `b` qui nous intéresse, qui est à chaque fois la somme des deux valeurs adjacente et précédente de la première colonne."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_06"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ex0o3_06\n",
    "\n",
    "init = int(input(\"Terme initial : \"))\n",
    "\n",
    "# on utilise l'option end= de print pour avoir de simples espaces\n",
    "# à la place des retours à la ligne, et donc tout sur la même ligne\n",
    "print(init, end=\" \")\n",
    "n = 0\n",
    "while n < 11:\n",
    "    init = 3*init\n",
    "    print(init, end=\" \")\n",
    "    n = n + 1\n",
    "print()  # pour avoir une fin de ligne finale après le dernier affichage"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_07"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_07\n",
    "\n",
    "i = 1\n",
    "while i < 51:\n",
    "    j = 13*i\n",
    "    if j%7 == 0:\n",
    "        print(j, end=\" \")  # l'option end=\" \" inhibe le retour à la ligne\n",
    "    i = i + 1\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_08"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_08\n",
    "\n",
    "tt = []\n",
    "ch = \"start\"\n",
    "while ch != \"\":\n",
    "    ch = input(\"Saisissez une valeur (<Entrée> pour arreter) : \")\n",
    "    if ch:    # ce test est faux si et seulement si la saisie est vide\n",
    "        tt.append(float(ch))\n",
    "\n",
    "print('\\n', tt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_09"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_09\n",
    "\n",
    "# saisie filtrée, on contrôle la validité des valeurs et on redemande\n",
    "# tant que l'utilisateur n'a pas saisi quelque chose de correct\n",
    "a = int(input(\"Entrez la limite inférieure : \"))\n",
    "b = int(input(\"Entrez la limite supérieure : \"))\n",
    "while b <= a:\n",
    "    b = int(input(\"Entrez la limite supérieure (b > a), SVP : \"))\n",
    "    \n",
    "s, n = 0, a\n",
    "while n <= b:\n",
    "    if (n%3 == 0) and (n%5 == 0):\n",
    "        s = s + n\n",
    "    n = n + 1\n",
    "\n",
    "print(\"\\nPremière somme, multiples de 3 et de 5 :\", s)\n",
    "\n",
    "s, n = 0, a\n",
    "while n <= b:\n",
    "    if (n%3 == 0) or (n%5 == 0):\n",
    "        s = s + n\n",
    "    n = n + 1\n",
    "\n",
    "print(\"Seconde somme, multiples de 3 ou de 5 :\", s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_10\n",
    "\n",
    "for i in range(1, 11):\n",
    "    print('*'*i)\n",
    "for i in range(1, 11):\n",
    "    print(' '*i + '*'*(10-i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_11\n",
    "\n",
    "for i in range(1, 11):\n",
    "    print('*'*i + ' '*(20-2*i) + '*'*i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_12\n",
    "\n",
    "for i in range(1, 11):\n",
    "    print('*'*(11-i) + ' '*(2*(i-1)) + '*'*(11-i))   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_13\n",
    "\n",
    "for i in range(1, 11):\n",
    "    print('*'*(11-i) + ' '*(2*(i-1)) + '*'*(11-i))\n",
    "for i in range(1, 11):\n",
    "    print('*'*i + ' '*(20-2*i) + '*'*i) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_14\n",
    "\n",
    "# On teste chaque possibilité demandée en itérant sur les différents cas :\n",
    "for a in [1, 2, 3, 15]:\n",
    "    if a != 2:\n",
    "        print(\"perdu\")\n",
    "    elif a == 3:\n",
    "        print(\"un instant, s.v.p.\")\n",
    "    else :\n",
    "        print(\"gagné\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_15\n",
    "\n",
    "a = int(input(\"Entrez la limite inférieure : \"))\n",
    "b = int(input(\"Entrez la limite supérieure : \"))\n",
    "\n",
    "# on va utiliser l'option `end=' '` de print pour placer les différents \n",
    "# affichages les uns à la suite des autres sur la même ligne\n",
    "s = 0           # initialisation de la somme recherchée\n",
    "n = a           # initialisation de la boucle\n",
    "premier = True  # cas special pour la première valeur affichée\n",
    "while n <= b:\n",
    "    if (n % 3 == 0) and (n % 5 == 0):\n",
    "        if premier: \n",
    "            print(n, end=' ')  # au premier, pas d'affichage du '+'\n",
    "            premier = False\n",
    "        else:\n",
    "            print(\"+\", n, end=' ')\n",
    "        s = s + n\n",
    "    n = n + 1\n",
    "print(\"=\", s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_16\n",
    "\n",
    "notes = []  # liste à construire\n",
    "somme = 0   # somme courante\n",
    "moy = 0.0   # en cas de saisie d'aucune valeur positive\n",
    "n = 0       # valeur positive quelconque pour initier la boucle\n",
    "while n >= 0 :\n",
    "    n = float(input(\"Entrez la note suivante (négative pour sortir) : \"))\n",
    "    if n < 0:\n",
    "        print(f\"\\nMoyenne finale = {moy:.2f} pour {len(notes)} notes\")\n",
    "    else:\n",
    "        notes.append(n)    # ajout d'une note à la liste\n",
    "        somme += n\n",
    "        nn = len(notes)    # nombre de notes déjà entrées\n",
    "        moy = somme / nn\n",
    "        print(f\"Moy = {moy:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_17"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_17\n",
    "\n",
    "print(\"Liste des carrés de 20 à 40 :\")\n",
    "for i in range(20, 41):\n",
    "    print(f'{i**2} ', end='')\n",
    "    \n",
    "print(\"\\n\\nListe des cubes de 20 à 40 :\")\n",
    "for i in range(20, 41):\n",
    "    print(f'{i**3} ', end='')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_18\n",
    "\n",
    "# saisies\n",
    "p = int(input(\"Entrez un entier p supérieur à 1 : \"))\n",
    "while p < 2:\n",
    "    p = int(input(\"Entrez un entier p supérieur à 1, SVP : \"))\n",
    "    \n",
    "q = int(input(\"Entrez un entier q supérieur à 1 : \"))\n",
    "while q < 2:\n",
    "    q = int(input(\"Entrez un entier q supérieur à 1, SVP : \"))\n",
    "    \n",
    "# affichages\n",
    "print()\n",
    "for x in range(1, p+1):\n",
    "    for y in range(1, q+1):\n",
    "        print(f\"({x}, {y}) \", end=\" \")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_19\n",
    "\n",
    "# saisies\n",
    "s = input(\"Entrez une chaîne s : \")\n",
    "t = input(\"Entrez une chaîne t : \")\n",
    "    \n",
    "## affichages\n",
    "print()\n",
    "for x in s:\n",
    "    for y in t:\n",
    "        print(f\"{x}{y} \", end=\" \")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_20\n",
    "\n",
    "# saisies\n",
    "p = int(input(\"Entrez un entier p supérieur à 1 : \"))\n",
    "while p < 2:\n",
    "    p = int(input(\"Entrez un entier p supérieur à 1, SVP : \"))\n",
    "    \n",
    "q = int(input(\"Entrez un entier q supérieur à 1 : \"))\n",
    "while q < 2:\n",
    "    q = int(input(\"Entrez un entier q supérieur à 1, SVP : \"))\n",
    "    \n",
    "# affichages\n",
    "print()\n",
    "for x in range(1, p+1):\n",
    "    for y in range(1, q+1):\n",
    "        if x <= y:\n",
    "            print(f\"({x}, {y}) \", end=\" \")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_21\n",
    "\n",
    "n = -1   # \n",
    "while n < 0:\n",
    "    n = int(input(\"Entrez un nombre entier positif :\"))\n",
    "if n % 2 == 0:\n",
    "    print(\"PAIR\")\n",
    "else:\n",
    "    print(\"IMPAIR\")\n",
    "    \n",
    "# autre écriture\n",
    "print(\"PAIR\") if n % 2 == 0 else print(\"IMPAIR\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_22\n",
    "\n",
    "# saisie filtrée de mot1\n",
    "mot1 = ''\n",
    "while mot1 == '':\n",
    "    mot1 = input(\"Saisissez un premier mot en minuscules, sans accent :\")\n",
    "    # vérification que chaque caractère est valide\n",
    "    for caractere in mot1:\n",
    "        if caractere not in ALPHABET:\n",
    "            # si un seul n'est pas valide, on recommence la saisie\n",
    "            mot1 = ''\n",
    "            break\n",
    "\n",
    "# saisie filtrée de mot2\n",
    "mot2 = ''\n",
    "while mot2 == '':\n",
    "    mot2 = input(\"Saisissez un second mot en minuscules, sans accent :\")\n",
    "    # vérification que chaque caractère est valide\n",
    "    for caractere in mot2:\n",
    "        if caractere not in ALPHABET:\n",
    "            # si un seul n'est pas valide, on recommence la saisie\n",
    "            mot2 = ''\n",
    "            break\n",
    "            \n",
    "# comparaison lexicographique (simplifiée) des chaines\n",
    "maxlen = min(len(mot1), len(mot2))\n",
    "for idx in range(maxlen):\n",
    "    if mot1[idx] == mot2[idx]:  # identiques… voir au caractère suivant\n",
    "        continue\n",
    "    elif mot1[idx] < mot2[idx]:\n",
    "        print(mot1, \"<\", mot2)\n",
    "        break\n",
    "    else:\n",
    "        print(mot1, \">\", mot2)\n",
    "        break\n",
    "else:\n",
    "    # spécificité Python, on ne passe ici QUE si on sort de la\n",
    "    # boucle normalement (sans passer par break)\n",
    "    # autre solution, positionner un drapeau lorsqu'on sort par\n",
    "    # break et le tester ensuite\n",
    "    if len(mot1) == len(mot2):\n",
    "        print(mot1, \"=\", mot2)\n",
    "    elif len(mot1) < len(mot2):\n",
    "        print(mot1, \"<\", mot2)\n",
    "    else:\n",
    "        print(mot1, \">\", mot2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_23"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_23\n",
    "\n",
    "PSEUIL = 2.23e5  # Pa\n",
    "VSEUIL = 7.41    # m3\n",
    "\n",
    "p = float(input(\"Pression courante (Pa) : \"))\n",
    "v = float(input(\"Volume courant (m3) : \"))\n",
    "\n",
    "if p > PSEUIL and v > VSEUIL:\n",
    "    print(\"Arrêt immédiat\")\n",
    "elif p > PSEUIL and v < VSEUIL:\n",
    "    print(\"Augmenter le volume de l'enceinte\")\n",
    "elif p < PSEUIL and v > VSEUIL:\n",
    "    print(\"Diminuer le volume de l'enceinte\")\n",
    "else:\n",
    "    print(\"Fonctionnement nominal\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo3_24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_24\n",
    "\n",
    "for i in range(1, 28):\n",
    "    if i > 10 and i % 2 == 1:  # entiers impairs supérieurs à 10\n",
    "        print(i, end=\" \")\n",
    "    if i >= 20:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_25\n",
    "\n",
    "for i in range(15):\n",
    "    if i % 2 == 1 or (i > 3 and i < 9):\n",
    "        continue\n",
    "    print(i, end=\" \")    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_26"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_26\n",
    "\n",
    "from math import sin, pi\n",
    "\n",
    "for x in range(-7, 8):\n",
    "    try:\n",
    "        print(f\"Pour {x}, sinc = {sin(pi * x) / sin(x):g}\")\n",
    "    except ZeroDivisionError:\n",
    "        print(\"Pour zéro sinc = 1\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_27"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_27\n",
    "\n",
    "# Import ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "from random import seed, randint\n",
    "\n",
    "# Programme pricipal =========================================================\n",
    "a, b = 1, 100  # bornes de la recherche\n",
    "print(f\"L'ordinateur tire un entier aléatoire entre {a} et {b}.\")\n",
    "print(\"\"\"À chaque tentative l'ordinateur vous indiquera si le nombre\n",
    "tiré est plus grand ou plus petit. Essayez de le deviner en un \n",
    "nombre minimum d'essais.\\n\"\"\")\n",
    "seed()  # Initialise le générateur de nombre aléatoire\n",
    "cible = randint(a, b)\n",
    "saisie = int(input(\"Devinez le nombre tiré: \"))\n",
    "nbessais = 1\n",
    "\n",
    "## Algorithme de dichotomie, c'est l'utilisateur qui doit choisir la valeur dans\n",
    "while cible != saisie:\n",
    "    if saisie > cible:\n",
    "        print(\"La cible est plus petite.\")\n",
    "    else:   # saisie < cible\n",
    "        print(\"La cible est plus grande.\")\n",
    "    saisie = int(input(\"Devinez le nombre tiré : \"))\n",
    "    nbessais += 1\n",
    "\n",
    "print(f\"\\ncible {cible} trouvée en {nbessais} tentatives.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo03_28"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo03_28\n",
    "\n",
    "# Import ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "from random import seed, randint\n",
    "\n",
    "# Programme pricipal =========================================================\n",
    "a, b = 0, 100  # bornes de la recherche\n",
    "print(f\"L'ordinateur tire un entier aléatoire entre {a} et {b}.\")\n",
    "seed()  # Initialise le générateur de nombre aléatoire\n",
    "cible = randint(a, b)\n",
    "\n",
    "## Algorithme de dichotomie\n",
    "ok = False\n",
    "while (not ok) and (a <= b):\n",
    "    m = (a + b) // 2\n",
    "    if m == cible:\n",
    "        ok = True\n",
    "    else:\n",
    "        if cible > m:\n",
    "            a = m + 1\n",
    "        else:\n",
    "            b = m - 1\n",
    "\n",
    "print(f\"cible  : {cible}\")\n",
    "print(f\"trouvé : {m}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
