From f2db4d1a6e639933e28616a81974037c0cfe754c Mon Sep 17 00:00:00 2001 From: nojhan Date: Wed, 14 Mar 2012 21:10:02 +0100 Subject: [PATCH] =?UTF-8?q?simplification=20et=20modularisation=20du=20jeu?= =?UTF-8?q?=20de=20devinette=20niveau=20z=C3=A9ro?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/devine_ordi_0.py | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/devine_ordi_0.py b/src/devine_ordi_0.py index 62ac8b0..f8c0cbd 100644 --- a/src/devine_ordi_0.py +++ b/src/devine_ordi_0.py @@ -8,14 +8,33 @@ print("Pense à un nombre entre",min,"et",max) essais = 4 -def devine_entre(nb_min,nb_max): - if nb_min == nb_max: +def deviner(nb_min,nb_max): + if nb_min == nb_max or nb_min > nb_max: print("Tu as triché !") - return random.randint(nb_min,nb_max) + hasard = random.randint(nb_min,nb_max) + return hasard + + +def repondu( reponse ): + if reponse == 0: + min = min + 1 + return False + + elif reponse == 1: + max = max - 1 + return False + + elif reponse == 2: + print("Enfin !") + return True + + else: + print("Tu dois répondre par 1, 2 ou 3 !") + return False while essais > 0: - devine = devine_entre(min,max) + devine = deviner(min,max) print("Je pense au nombre",devine,", est-il:") print("0: trop petit ?") @@ -23,17 +42,7 @@ while essais > 0: print("2: le bon numéro ?") reponse = int( input() ) - if reponse == 0: - min = min + 1 - - elif reponse == 1: - max = max - 1 - - elif reponse == 2: - print("Enfin !") + trouve = repondu( reponse ) + if trouve == True: break - else: - print("Tu dois répondre par 1, 2 ou 3 !") - -