summaryrefslogtreecommitdiff
path: root/nbmystere.py
blob: cab65598dd88a6c55dd9b22c7fda9b09bfc29d95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
"""module du jeu du nombre mystère"""
import sys
import random


def jouer(limite=100):
    """fonction jouer avec en argument la limite supérieure de l'intervalle"""
    nb = 0
    cpteur = 0
    secret = random.randrange(1, limite)
    # print(f"debug : {secret = }")

    while nb != secret:
        cpteur = cpteur + 1
        try:
            nb = int(input(f"Trouvez le nombre secret entre 1 et {limite} : "))
        except ValueError:
            print("On a dit un nombre !")
            continue
        if nb > secret:
            print("Trop grand")
        elif nb < secret:
            print("Trop petit")

    print(f"Gagné en {cpteur} tentatives")


if __name__ == "__main__":
    try:
        limite = int(sys.argv[1])
        jouer(limite)
    except IndexError:
        print(f"{sys.argv[0] } nécessite une limite en argument")