1A.1 - Deviner un nombre aléatoire (correction)#

Links: notebook, html, python, slides, GitHub

On reprend la fonction introduite dans l’énoncé et qui permet de saisir un nombre.

import random
nombre = input("Entrez un nombre")
Entrez un nombre7
nombre
'7'

Q1 : Ecrire une jeu dans lequel python choisi aléatoirement un nombre entre 0 et 100, et essayer de trouver ce nombre en 10 étapes.

n = random.randint(0,100)
appreciation = "?"
while True:
    var = input("Entrez un nombre")
    var = int(var)
    if var < n :
        appreciation = "trop bas"
        print(var, appreciation)

    else :
        appreciation = "trop haut"
        print(var, appreciation)
    if var == n:
        appreciation = "bravo !"
        print(var, appreciation)
        break
Entrez un nombre7
7 trop bas
Entrez un nombre10
10 trop bas
Entrez un nombre100
100 trop haut
Entrez un nombre50
50 trop bas
Entrez un nombre75
75 trop haut
Entrez un nombre60
60 trop haut
Entrez un nombre55
55 trop haut
Entrez un nombre53
53 trop haut
Entrez un nombre52
52 trop haut
Entrez un nombre51
51 trop haut
51 bravo !

Q2 : Transformer ce jeu en une fonction jeu(nVies)nVies est le nombre d’itérations maximum.

import random
n = random.randint(0,100)
vies = 10
appreciation = "?"
while vies > 0:
    var = input("Entrez un nombre")
    var = int(var)
    if var < n :
        appreciation = "trop bas"
        print(vies, var, appreciation)
    else :
        appreciation = "trop haut"
        print(vies, var, appreciation)
    if var == n:
        appreciation = "bravo !"
        print(vies, var, appreciation)
        break

    vies -= 1
Entrez un nombre50
10 50 trop bas
Entrez un nombre75
9 75 trop haut
Entrez un nombre60
8 60 trop bas
Entrez un nombre65
7 65 trop bas
Entrez un nombre70
6 70 trop bas
Entrez un nombre73
5 73 trop haut
Entrez un nombre72
4 72 trop haut
Entrez un nombre71
3 71 trop haut
3 71 bravo !

Q3 : Adapter le code pour faire une classe joueur avec une méthode jouer, où un joueur est défini par un pseudo et son nombre de vies. Faire jouer deux joueurs et déterminer le vainqueur.

class joueur:
    def __init__(self, vies, pseudo):
        self.vies = vies
        self.pseudo = pseudo

    def jouer(self):
        appreciation = "?"
        n = random.randint(0,100)
        while self.vies > 0:
            message = appreciation + " -- " + self.pseudo + " : " + str(self.vies) + " vies restantes. Nombre choisi : "
            var = input(message)
            var = int(var)
            if var < n :
                appreciation = "trop bas"
                print(vies, var, appreciation)
            else :
                appreciation = "trop haut"
                print(vies, var, appreciation)
            if var == n:
                appreciation = "bravo !"
                print(vies, var, appreciation)
                break

            self.vies -= 1

# Initialisation des deux joueurs
j1 = joueur(10, "joueur 1")
j2 = joueur(10, "joueur 2")

# j1 et j2 jouent
j1.jouer()
j2.jouer()

# Nombre de vies restantes à chaque joueur
print("Nombre de vies restantes à chaque joueur")
print(j1.pseudo  + " : " + str(j1.vies) + " restantes")
print(j2.pseudo  + " : " + str(j2.vies) + " restantes")

# Résultat de la partie
print("Résultat de la partie")
if j1.vies < j2.vies:
    print(j1.pseudo + "a gagné la partie")
elif j1.vies == j2.vies:
    print("match nul")
else: print(j2.pseudo + " a gagné la partie")
? -- joueur 1 : 10 vies restantes. Nombre choisi : 50
3 50 trop haut
trop haut -- joueur 1 : 9 vies restantes. Nombre choisi : 25
3 25 trop haut
trop haut -- joueur 1 : 8 vies restantes. Nombre choisi : 10
3 10 trop haut
trop haut -- joueur 1 : 7 vies restantes. Nombre choisi : 5
3 5 trop bas
trop bas -- joueur 1 : 6 vies restantes. Nombre choisi : 7
3 7 trop haut
3 7 bravo !
? -- joueur 2 : 10 vies restantes. Nombre choisi : 50
3 50 trop haut
trop haut -- joueur 2 : 9 vies restantes. Nombre choisi : 25
3 25 trop bas
trop bas -- joueur 2 : 8 vies restantes. Nombre choisi : 30
3 30 trop haut
3 30 bravo !
Nombre de vies restantes à chaque joueur
joueur 1 : 6 restantes
joueur 2 : 8 restantes
Résultat de la partie
joueur 1a gagné la partie