{"id":8136,"date":"2023-01-07T17:15:41","date_gmt":"2023-01-07T16:15:41","guid":{"rendered":"https:\/\/www.mathweb.fr\/euclide\/?p=8136"},"modified":"2023-01-09T17:14:26","modified_gmt":"2023-01-09T16:14:26","slug":"le-jeu-du-plus-ou-moins-et-python","status":"publish","type":"post","link":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/","title":{"rendered":"Le jeu du plus ou moins et Python"},"content":{"rendered":"\n<p>Le jeu du plus ou moins est un jeu consistant \u00e0 choisir un nombre entre 1 et 100 et \u00e0 le faire deviner \u00e0 une tierce personne. Penchons-nous sur son impl\u00e9mentation en Python.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-white ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Au menu sur cette page...<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#Jeu_du_plus_ou_moins_les_regles\" >Jeu du plus ou moins: les r\u00e8gles<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#Les_regles\" >Les r\u00e8gles<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#Un_exemple_dimplementation_en_Python\" >Un exemple d&#8217;impl\u00e9mentation en Python<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#Jeu_du_plus_ou_moins_une_strategie_mathematique\" >Jeu du plus ou moins: une strat\u00e9gie math\u00e9matique<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#La_strategie_la_dichotomie\" >La strat\u00e9gie: la dichotomie<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#Un_exemple\" >Un exemple<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#Implementation_en_Python\" >Impl\u00e9mentation en Python<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#Aller_plus_loin\" >Aller plus loin<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#Calculs_mathematiques\" >Calculs math\u00e9matiques<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Jeu_du_plus_ou_moins_les_regles\"><\/span>Jeu du plus ou moins: les r\u00e8gles<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Les_regles\"><\/span>Les r\u00e8gles<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Le ma\u00eetre du jeu choisit un nombre entier compris entre 1 et 100: c&#8217;est le nombre myst\u00e8re. Il y a donc 100 nombres possibles.<\/p>\n\n\n\n<p>Le joueur propose un nombre: si le nombre myst\u00e8re est plus grand que la proposition, le ma\u00eetre du jeu r\u00e9pond : &#8220;plus&#8221;, sinon, il r\u00e9pond &#8220;moins&#8221;.<\/p>\n\n\n\n<p>Le but du jeu est de deviner le nombre myst\u00e8re en un minimum de coups.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Un_exemple_dimplementation_en_Python\"><\/span>Un exemple d&#8217;impl\u00e9mentation en Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Voici une proposition de programme:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from random import randint\n\ndef jeu():\n    x = randint(1,100)\n    n = 0\n    coups = 0\n    while x != n:\n        n = int( input('Votre proposition: ') )\n        coups += 1\n        if x &lt; n: print(\"\\033[92m C'est moins.\\033[0m\")\n        elif x > n: print(\"\\033[93m C'est plus.\\033[0m\")\n        else: print(f\"\\033[91m Gagn\u00e9 en {coups} coups.\\033[0m\")<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/image.png\" data-fancybox=\"gallery\"><img loading=\"lazy\" decoding=\"async\" width=\"239\" height=\"150\" src=\"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/image.png\" alt=\"jeu plus ou moins python\" class=\"wp-image-8137\"\/><\/a><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Jeu_du_plus_ou_moins_une_strategie_mathematique\"><\/span>Jeu du plus ou moins: une strat\u00e9gie math\u00e9matique<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"La_strategie_la_dichotomie\"><\/span>La strat\u00e9gie: la dichotomie<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Il existe une mani\u00e8re de minimiser le nombres de propositions quand on joue \u00e0 ce jeu.<\/p>\n\n\n\n<p>En effet, si on propose au hasard, sans strat\u00e9gie, n&#8217;importe quels nombres, cela risque d&#8217;\u00eatre long. C&#8217;est pourquoi avoir une strat\u00e9gie s&#8217;impose. Ici, nous allons raisonner par <em><a href=\"https:\/\/www.mathweb.fr\/euclide\/dichotomie\/\" target=\"_blank\" rel=\"noreferrer noopener\">dichotomie<\/a><\/em>.<\/p>\n\n\n\n<p>Cela consiste, \u00e0 chaque \u00e9tape, \u00e0 choisir le milieu de l&#8217;intervalle o\u00f9 se trouve le nombre.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Un_exemple\"><\/span>Un exemple<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Supposons que le ma\u00eetre du jeu choisisse le nombre 23.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>On commence par le milieu de l&#8217;intervalle [1;100] : 50. Le ma\u00eetre du jeu r\u00e9pond alors : &#8220;moins&#8221; car 23 &lt; 50.<\/li>\n\n\n\n<li>On sait maintenant que le nombre myst\u00e8re est dans l&#8217;intervalle [1 ; 50[; on choisit donc son milieu: 25. Le ma\u00eetre du jeu r\u00e9ponds &#8220;moins&#8221;.<\/li>\n\n\n\n<li>Le nombre doit donc \u00eatre dans [1 ; 25[; on choisit le &#8220;milieu&#8221;, tout du moins la partie enti\u00e8re du milieu, soit 12. Le ma\u00eetre du jeu r\u00e9ponds: &#8220;plus&#8221;.<\/li>\n\n\n\n<li>Maintenant, on sait que le nombre \u00e0 deviner est dans ]12 ; 25[; on choisit la partie enti\u00e8re du milieu, soit \\(E\\left(\\frac{12+25}{2}\\right)=18\\) . Le ma\u00eetre du jeu r\u00e9pond alors : &#8220;plus&#8221;.<\/li>\n\n\n\n<li>On choisit alors la partie enti\u00e8re du milieu de ]18;25[, \u00e0 savoir \\(E\\left(\\frac{18+25}{2}\\right)=21\\). &#8220;c&#8217;est plus&#8221;.<\/li>\n\n\n\n<li>Le nombre myst\u00e8re est donc dans ]21;25[; on propose alors 23, le milieu de cet intervalle&#8230; et on gagne!<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Implementation_en_Python\"><\/span>Impl\u00e9mentation en Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Nous allons demander \u00e0 l&#8217;ordinateur de choisir un nombre compris entre 1 et 100; nous allons le stocker dans la variable x et nous allons cr\u00e9er une variable &#8220;coups&#8221; pour compter le nombre de propositions.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from random import randint\nx = randint(1,100)\ncoups = 0<\/pre>\n\n\n\n<p>Nous allons maintenant demander \u00e0 l\u2019ordinateur de simuler les propositions.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">n = 0\na, b = 1, 100\nwhile n != x:\n    coups += 1\n    n = (a+b)\/\/2\n    if x > n: a = n\n    if x &lt; n: b = n\n    print( n )<\/pre>\n\n\n\n<p>Cette proposition n&#8217;est pas encore optimale car si x = 100, le programme bloque sur &#8220;99&#8221;&#8230; et pour cause: \\( E\\left(\\frac{99+100}{2}\\right)=99.\\)<\/p>\n\n\n\n<p>Il faut donc distinguer le cas o\u00f9 n = a et celui o\u00f9 n est diff\u00e9rent de a.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">n = 0\na, b = 1, 100\nwhile n != x:\n    coups += 1\n    n = (a+b)\/\/2\n    if x > n:\n        if n != a: a = n\n        else: n = b\n    elif x &lt; n: b = n\n    print( f'a={a}, b={b}, n={n}, coups = {coups}' )<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>a=1, b=50, n=50, coups = 1\na=1, b=25, n=25, coups = 2\na=1, b=13, n=13, coups = 3\na=7, b=13, n=7, coups = 4\na=10, b=13, n=10, coups = 5\na=10, b=13, n=11, coups = 6<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Aller_plus_loin\"><\/span>Aller plus loin<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>J&#8217;ai impl\u00e9menter une fonction <em><strong>game(a,b)<\/strong><\/em> qui retourne le nombre de coups n\u00e9cessaires pour trouver le nombre al\u00e9atoirement choisi entre deux entiers <em>a<\/em> et <em>b<\/em>  en utilisant la dichotomie. Ensuite, j&#8217;ai effectu\u00e9 10000 simulations, puis ai calcul\u00e9 la moyenne des coups n\u00e9cessaires. J&#8217;ai fait cela pour a = 1 et pour \\(b = 10^n\\), avec n variant de 1 \u00e0 10. Voici les r\u00e9sultats:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">Valeurs de n avec \\(b = 10^n\\)<\/th><th class=\"has-text-align-center\" data-align=\"center\">Nombre moyens de coups n\u00e9cessaires<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">3.03<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td class=\"has-text-align-center\" data-align=\"center\">5.82<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td class=\"has-text-align-center\" data-align=\"center\">8.99<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">4<\/td><td class=\"has-text-align-center\" data-align=\"center\">12.35<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">5<\/td><td class=\"has-text-align-center\" data-align=\"center\">15.69<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">6<\/td><td class=\"has-text-align-center\" data-align=\"center\">18.95<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">7<\/td><td class=\"has-text-align-center\" data-align=\"center\">22.33<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">8<\/td><td class=\"has-text-align-center\" data-align=\"center\">25.66<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">9<\/td><td class=\"has-text-align-center\" data-align=\"center\">28.93<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">10<\/td><td class=\"has-text-align-center\" data-align=\"center\">32.27<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/image-1.png\" data-fancybox=\"gallery\"><img loading=\"lazy\" decoding=\"async\" width=\"557\" height=\"429\" src=\"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/image-1.png\" alt=\"\" class=\"wp-image-8138\" srcset=\"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/image-1.png 557w, https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/image-1-300x231.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>Une belle corr\u00e9lation lin\u00e9aire s&#8217;offre \u00e0 nous.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from random import randint\nfrom matplotlib.pyplot import axes, scatter, show, plot, legend, xlabel, ylabel, title\n\ndef jeu():\n    x = randint(1,100)\n    n = 0\n    coups = 0\n    while x != n:\n        n = int( input('Votre proposition: ') )\n        coups += 1\n        if x &lt; n: print(\"\\033[92m C'est moins.\\033[0m\")\n        elif x > n: print(\"\\033[93m C'est plus.\\033[0m\")\n        else: print(f\"\\033[91m Gagn\u00e9 en {coups} coups.\\033[0m\")\n\n\ndef game(a,b):\n    x = randint(a,b)\n    coups = 0\n    n = 0\n\n    while n != x:\n        coups += 1\n        n = (a+b)\/\/2\n        if x > n:\n            if n != a: a = n\n            else: n = b\n        elif x &lt; n: b = n\n        \n    return coups\n\nx, y = [],[]\n\nfor e in range(1,11):\n    S = 0\n    for _ in range(10000):\n        S += game(1,10**e)\n    x.append(e)\n    y.append(S\/10000)\n    print( f'e = {e} , moyenne = {S \/ 10000}' )\n    \n\nrepere = axes()\nrepere.grid() # dessiner une grille pour une meilleur lisibilit\u00e9 du graphe\nscatter(x,y)\nshow()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Calculs_mathematiques\"><\/span>Calculs math\u00e9matiques<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Notons X la variable al\u00e9atoire repr\u00e9sentant le nombre de coups n\u00e9cessaires pour gagner avec la strat\u00e9gie de la dichotomie.<\/p>\n\n\n\n<p>Alors, X = {1;2;3;4;5;6;7;8}.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\\( P(X = 1) = \\frac{1}{100}\\) : c&#8217;est le cas o\u00f9 le nombre myst\u00e8re est 50; il y a bien 1 chance sur 100 de le choisir.<\/li>\n\n\n\n<li>\\( P(X = 2) = \\frac{2}{100}\\) : c&#8217;est le cas o\u00f9 le nombre myst\u00e8re est 25 ou 75.<\/li>\n\n\n\n<li>\\( P(X = 3) = \\frac{4}{100}\\) : c&#8217;est le cas si <em>x<\/em> = 12 ou 37 ou 62 ou 87.<\/li>\n\n\n\n<li>\\( P(X = 4) = \\frac{8}{100}\\): c&#8217;est le cas si <em>x<\/em> = 6, 18, 31, 43, 56, 68, 81 ou 93.<\/li>\n\n\n\n<li>\\( P(X = 5) = \\frac{16}{100}: c&#8217;est le cas pour <em>x<\/em> = 3, 9, 15, 21, 28, 34, 40, 46, 53, 59, 65, 71, 78, 84, 90, 96.<\/li>\n\n\n\n<li>\\( P(X = 6) = \\frac{32}{100}\\): c&#8217;est le cas pour <em>x<\/em> = 1, 4, 7, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41, 44, 48, 51, 54, 57, 60, 63, 66, 69, 73, 76, 79, 82, 85, 88, 91, 94, 98.<\/li>\n\n\n\n<li>\\( P(X = 7) = \\frac{36}{100} \\): c&#8217;est le cas pour <em>x<\/em> = 2, 5, 8, 11, 14, 17, 20, 22, 24, 27, 30, 33, 36, 39, 42, 45, 47, 49, 52, 55, 58, 61, 64, 67, 70, 72, 74, 77, 80, 83, 86, 89, 92, 95, 97, 99.<\/li>\n\n\n\n<li>\\(P(X = 8) = \\frac{1}{100}\\) pour <em>x<\/em> = 100.<\/li>\n<\/ul>\n\n\n\n<p>On peut donc \u00e9tablir la loi de probabilit\u00e9 de X:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\\(X=k\\)<\/td><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td class=\"has-text-align-center\" data-align=\"center\">4<\/td><td class=\"has-text-align-center\" data-align=\"center\">5<\/td><td class=\"has-text-align-center\" data-align=\"center\">6<\/td><td class=\"has-text-align-center\" data-align=\"center\">7<\/td><td class=\"has-text-align-center\" data-align=\"center\">8<\/td><\/tr><tr><td>\\(P(X=k)\\)<\/td><td class=\"has-text-align-center\" data-align=\"center\">0,01<\/td><td class=\"has-text-align-center\" data-align=\"center\">0,02<\/td><td class=\"has-text-align-center\" data-align=\"center\">0,04<\/td><td class=\"has-text-align-center\" data-align=\"center\">0,08<\/td><td class=\"has-text-align-center\" data-align=\"center\">0,16<\/td><td class=\"has-text-align-center\" data-align=\"center\">0,32<\/td><td class=\"has-text-align-center\" data-align=\"center\">0,36<\/td><td class=\"has-text-align-center\" data-align=\"center\">0,01<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>L&#8217;esp\u00e9rance math\u00e9matique de X est donc:$$E(X)=0,01+2\\times0,02+3\\times0,04+\\cdots+8\\times0,01=5,81.$$Cette valeur correspond bien \u00e0 notre simulation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Le jeu du plus ou moins est un jeu consistant \u00e0 choisir un nombre entre 1 et 100 et \u00e0 le faire deviner \u00e0 une tierce personne. Penchons-nous sur son impl\u00e9mentation en Python.<\/p>\n","protected":false},"author":1,"featured_media":8139,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-8136","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Le jeu du plus ou moins et Python - Mathweb.fr<\/title>\n<meta name=\"description\" content=\"Le jeu du plus ou moins est un jeu consistant \u00e0 choisir un nombre entre 1 et 100 et \u00e0 le faire deviner \u00e0 une tierce personne.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Le jeu du plus ou moins et Python - Mathweb.fr\" \/>\n<meta property=\"og:description\" content=\"Le jeu du plus ou moins est un jeu consistant \u00e0 choisir un nombre entre 1 et 100 et \u00e0 le faire deviner \u00e0 une tierce personne.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Mathweb.fr\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-07T16:15:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-09T16:14:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/head-articles-python-jeu-plus-moins.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"740\" \/>\n\t<meta property=\"og:image:height\" content=\"198\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"St\u00e9phane Pasquet\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"St\u00e9phane Pasquet\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/\"},\"author\":{\"name\":\"St\u00e9phane Pasquet\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#\\\/schema\\\/person\\\/e4d3bb07968238378f0d5052a70dcd69\"},\"headline\":\"Le jeu du plus ou moins et Python\",\"datePublished\":\"2023-01-07T16:15:41+00:00\",\"dateModified\":\"2023-01-09T16:14:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/\"},\"wordCount\":691,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#\\\/schema\\\/person\\\/e4d3bb07968238378f0d5052a70dcd69\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/head-articles-python-jeu-plus-moins.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/\",\"url\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/\",\"name\":\"Le jeu du plus ou moins et Python - Mathweb.fr\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/head-articles-python-jeu-plus-moins.jpg\",\"datePublished\":\"2023-01-07T16:15:41+00:00\",\"dateModified\":\"2023-01-09T16:14:26+00:00\",\"description\":\"Le jeu du plus ou moins est un jeu consistant \u00e0 choisir un nombre entre 1 et 100 et \u00e0 le faire deviner \u00e0 une tierce personne.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/head-articles-python-jeu-plus-moins.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2023\\\/01\\\/head-articles-python-jeu-plus-moins.jpg\",\"width\":740,\"height\":198,\"caption\":\"jeu plus ou moins Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/2023\\\/01\\\/07\\\/le-jeu-du-plus-ou-moins-et-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Le jeu du plus ou moins et Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#website\",\"url\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/\",\"name\":\"Mathweb.fr\",\"description\":\"Math\u00e9matiques, LaTeX et Python\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#\\\/schema\\\/person\\\/e4d3bb07968238378f0d5052a70dcd69\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/#\\\/schema\\\/person\\\/e4d3bb07968238378f0d5052a70dcd69\",\"name\":\"St\u00e9phane Pasquet\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-logo-mathweb.webp\",\"url\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-logo-mathweb.webp\",\"contentUrl\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-logo-mathweb.webp\",\"width\":74,\"height\":77,\"caption\":\"St\u00e9phane Pasquet\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.mathweb.fr\\\/euclide\\\/wp-content\\\/uploads\\\/2025\\\/06\\\/cropped-logo-mathweb.webp\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Le jeu du plus ou moins et Python - Mathweb.fr","description":"Le jeu du plus ou moins est un jeu consistant \u00e0 choisir un nombre entre 1 et 100 et \u00e0 le faire deviner \u00e0 une tierce personne.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/","og_locale":"fr_FR","og_type":"article","og_title":"Le jeu du plus ou moins et Python - Mathweb.fr","og_description":"Le jeu du plus ou moins est un jeu consistant \u00e0 choisir un nombre entre 1 et 100 et \u00e0 le faire deviner \u00e0 une tierce personne.","og_url":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/","og_site_name":"Mathweb.fr","article_published_time":"2023-01-07T16:15:41+00:00","article_modified_time":"2023-01-09T16:14:26+00:00","og_image":[{"width":740,"height":198,"url":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/head-articles-python-jeu-plus-moins.jpg","type":"image\/jpeg"}],"author":"St\u00e9phane Pasquet","twitter_card":"summary_large_image","twitter_misc":{"\u00c9crit par":"St\u00e9phane Pasquet","Dur\u00e9e de lecture estim\u00e9e":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#article","isPartOf":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/"},"author":{"name":"St\u00e9phane Pasquet","@id":"https:\/\/www.mathweb.fr\/euclide\/#\/schema\/person\/e4d3bb07968238378f0d5052a70dcd69"},"headline":"Le jeu du plus ou moins et Python","datePublished":"2023-01-07T16:15:41+00:00","dateModified":"2023-01-09T16:14:26+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/"},"wordCount":691,"commentCount":1,"publisher":{"@id":"https:\/\/www.mathweb.fr\/euclide\/#\/schema\/person\/e4d3bb07968238378f0d5052a70dcd69"},"image":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/head-articles-python-jeu-plus-moins.jpg","articleSection":["Python"],"inLanguage":"fr-FR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/","url":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/","name":"Le jeu du plus ou moins et Python - Mathweb.fr","isPartOf":{"@id":"https:\/\/www.mathweb.fr\/euclide\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#primaryimage"},"image":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/head-articles-python-jeu-plus-moins.jpg","datePublished":"2023-01-07T16:15:41+00:00","dateModified":"2023-01-09T16:14:26+00:00","description":"Le jeu du plus ou moins est un jeu consistant \u00e0 choisir un nombre entre 1 et 100 et \u00e0 le faire deviner \u00e0 une tierce personne.","breadcrumb":{"@id":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/"]}]},{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#primaryimage","url":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/head-articles-python-jeu-plus-moins.jpg","contentUrl":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2023\/01\/head-articles-python-jeu-plus-moins.jpg","width":740,"height":198,"caption":"jeu plus ou moins Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mathweb.fr\/euclide\/2023\/01\/07\/le-jeu-du-plus-ou-moins-et-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.mathweb.fr\/euclide\/"},{"@type":"ListItem","position":2,"name":"Le jeu du plus ou moins et Python"}]},{"@type":"WebSite","@id":"https:\/\/www.mathweb.fr\/euclide\/#website","url":"https:\/\/www.mathweb.fr\/euclide\/","name":"Mathweb.fr","description":"Math\u00e9matiques, LaTeX et Python","publisher":{"@id":"https:\/\/www.mathweb.fr\/euclide\/#\/schema\/person\/e4d3bb07968238378f0d5052a70dcd69"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mathweb.fr\/euclide\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":["Person","Organization"],"@id":"https:\/\/www.mathweb.fr\/euclide\/#\/schema\/person\/e4d3bb07968238378f0d5052a70dcd69","name":"St\u00e9phane Pasquet","image":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2025\/06\/cropped-logo-mathweb.webp","url":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2025\/06\/cropped-logo-mathweb.webp","contentUrl":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2025\/06\/cropped-logo-mathweb.webp","width":74,"height":77,"caption":"St\u00e9phane Pasquet"},"logo":{"@id":"https:\/\/www.mathweb.fr\/euclide\/wp-content\/uploads\/2025\/06\/cropped-logo-mathweb.webp"}}]}},"_links":{"self":[{"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/posts\/8136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/comments?post=8136"}],"version-history":[{"count":0,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/posts\/8136\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/media\/8139"}],"wp:attachment":[{"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/media?parent=8136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/categories?post=8136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mathweb.fr\/euclide\/wp-json\/wp\/v2\/tags?post=8136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}