0% ont trouvé ce document utile (0 vote)
8 vues4 pages

Jeu de dés en Python pour 2ème année

Le document présente un jeu de dés pour les élèves de 2ème année scientifiques, où les joueurs gagnent en fonction des résultats des dés lancés. Deux variantes du jeu sont décrites : la première où il faut obtenir le même nombre sur les deux dés, et la seconde où la somme des points doit être paire. Des algorithmes et des exemples en Python sont fournis pour illustrer les règles et les conditions du jeu.

Transféré par

Med Habib Rammeh
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
8 vues4 pages

Jeu de dés en Python pour 2ème année

Le document présente un jeu de dés pour les élèves de 2ème année scientifiques, où les joueurs gagnent en fonction des résultats des dés lancés. Deux variantes du jeu sont décrites : la première où il faut obtenir le même nombre sur les deux dés, et la seconde où la somme des points doit être paire. Des algorithmes et des exemples en Python sont fournis pour illustrer les règles et les conditions du jeu.

Transféré par

Med Habib Rammeh
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd

Classe : 2ème Année Scientifiques Activité 07 : Jeu de dés

Activité 07_1

Jeu de dés
Le jeu consiste à lancer deux dés simultanément. Tu
gagnes si le nombre obtenu du 1er dés est celui de 2ème dés.

Exemples d’exécution :

Exemple 1 Exemple 2

Début

Générer deux entiers x et y au hasard entre 1 et 6

Afficher : x et y

Oui Si Non
x=y

Afficher : 'Bravo Tu as gagné' Afficher : 'Désolé Tu as perdu'

Fin

Algorithme Python

PROGRAMME Jeu_de_dés from random import*


Début x=randint(1,6)
x←Aléa(1,6) y=randint(1,6)
y← Aléa (1,6) print('tu as obtenu:',x,'et',y)
Ecrire('tu as obtenu:',x,'et',y) if x == y :
si x = y alors print('bravo! tu as gagné')
Ecrire('bravo! tu as gagné') else:
Sinon print('désolé! tu as perdu')
Ecrire ('désolé! tu as perdu')
Fin si Objet T/N
Fin x,y entiers

Enseignant : LAYOUNI Kais Page 1 sur 4


Classe : 2ème Année Scientifiques Activité 07 : Jeu de dés

Activité 07_2

Jeu de dés
Modifier le principe du jeu : tu gagnes si la somme des points
obtenue de deux dés est paire.

Exemples d’exécution :

Exemple 1 Exemple 2

Algorithme Python

PROGRAMME Jeu_de_dés from random import*


Début x=randint(1,6)
x←Aléa(1,6) y=randint(1,6)
y← Aléa (1,6) print('tu as obtenu:',x,'et',y)
Ecrire('tu as obtenu:',x,'et',y) s= x + y
s← x + y print('ton score est :',s)
Ecrire('ton score est :',s) if s % 2 == 0 :
si s mod 2 = 0 alors si s ∈ {2,4,6,8,10,12} alors print('bravo! tu as gagné')
Ecrire('bravo! tu as gagné') else:
Sinon print('désolé! tu as perdu')
Ecrire ('désolé ! tu as perdu')
Fin si Objet T/N
Fin x,y,s entiers

Retenons

❖ Fonction Aléa : Aléa permet de générer un entier au hasard.

Algorithme Python

variable ← Aléa(Vi,Vf) from random import *


variable = randint(Vi,Vf)

Exemple : x = randint(10,20)
x est un entier au hasard entre 10 et 20

Enseignant : LAYOUNI Kais Page 2 sur 4


Classe : 2ème Année Scientifiques Activité 07 : Jeu de dés

❖ Division entière :

Division entière

Algorithme Python Exemple (algo)


NB : 7 / 2 = 3.5
Division décimale
a div b a // b 7 div 2 donne 3 8 / 2 = 4.0

a mod b a%b 7 mod 2 donne 1 Comparaison

Algorithme Python
❖ Type booléen :
a=b a == b
• Le résultat de la comparaison est de type booléen (bool)
• Une variable de type booléen reçoit la valeur : a≠b a != b
vrai (True) ou faux (False)
• Exemples : x← 5 < 2 a>b a>b
y← 2 = 2
✓ x← faux a<b a<b
✓ y←vrai
✓ Donc x et y sont de type booléen a≥b a >= b

Opérateurs logiques a≤b a <= b


NON(a) a ET b a OU b
a b a ∈ {v1,v2,v3} a in { v1,v2,v3}
not(a) a and b a or b
a ∈ [Vi,Vf] a in [Vi,Vf]
Faux Faux V F F
Faux Vrai V F V
a ∉ {v1,v2,v3} a not in {v1,v2,v3}
Vrai Vrai F V V
a ∉ [Vi,Vf] a not in [Vi,Vf]
Vrai Faux F F V
**********************************************************
Application

Valeur du
Instructions (Algo) Instructions (Python) Type du résultat
Algorithme Python résultat

Entier int a ← ent(4.85) a = int(4.85) 4 Entier

Réel float
b ← arrondi(14.15) b = round(14.15) 14 Entier
Booléen bool
c←b/2 c=b/2 7.0 Réel

d ← b div a d = b // a 3 Entier

e ← aléa(d,a)<1 e = randint(d,a)<1 False Booléen

f ← a = (b mod a) + 2 f = a == (b % a) + 2 True Booléen

g ← f ou vrai et non(f) g = f or True and not(f) True Booléen

Enseignant : LAYOUNI Kais Page 3 sur 4


Classe : 2ème Année Scientifiques Activité 07 : Jeu de dés

❖ Conditionnelle complète :

• On utilise une structure conditionnelle si


l’exécution d’un traitement dépend d’une
condition.
• Si la condition est vraie, le traitement1 sera
exécuté.
• Si la condition est fausse, le traitement2
sera exécuté.

Algorithme Python

Si condition alors if condition :


traitement1 traitement1
Sinon else :
traitement 2 traitement2
FinSi

**********************************************************
Application
Compléter le tableau suivant en déterminant le traitement de chaque problème.

Problème Instructions (Algo) Instructions (Python)

Ecrire('donner un réel:'), Lire(x)


x=float(input('donner un réel:'))
Si x > 0 alors
Vérifier le signe d’un réel x en if x > 0 :
Ecrire ('Positif')
affichant le message « Positif » print ('Positif')
Sinon
ou « négatif » else:
Ecrire ('négatif')
print ('négatif')
Finsi

Ecrire('donner un entier:'), Lire(x)


Soient x et y deux entiers. x=int(input('donner un entier:'))
Ecrire('donner un entier:'), Lire(y)
✓ Dans le cas où x est y=int(input('donner un entier:'))
Si x < y alors
inférieur à y, ajouter un if x < y :
x←x+1
point à x et retirer un point x=x+1
y←y-1
à y. y=y-1
Sinon
✓ Dans le cas contraire, else :
x←x-1
ajouter un point à y et x=x-1
y←y+1
retirer un point à x. y=y+1
Finsi

Ecrire('donner un entier non nul:'), Lire(a)


x=int(input('donner un entier:'))
Soient a et b deux entiers non Ecrire('donner un entier non nul:'), Lire(b)
y=int(input('donner un entier:'))
nuls. si (a+b) mod 2=0 et (a mod b=0 ou b mod a=0) alors
if (a+b)%2==0 and (a%b==0 or b%a==0) :
a et b sont dit amis si leur Ecrire('amis')
print('amis')
somme est paire et l’un est sinon
else:
divisible par l’autre Ecrire('non amis')
print('non amis')
finsi

Enseignant : LAYOUNI Kais Page 4 sur 4

Common questions

Alimenté par l’IA

The modification of the game's rule—from winning based on the dice being equal to winning if the sum is even—affects the probability of winning. With the original rule, only six combinations result in a win (when both numbers are the same). However, with the even sum rule, there are more combinations since even sums can result from multiple different pairings, increasing the odds of winning .

Conditional structures can evaluate relationships by using 'if-else' conditions that first check whether the sum of 'a' and 'b' is even using '(a+b) % 2 == 0', then verify divisibility with '(a % b == 0 or b % a == 0)'. These conditions are combined using logical operators to ascertain if 'a' and 'b' can be categorized as 'amis'. Each condition must be met for them to be considered 'amis', demonstrating how compound conditions work .

In the first game variant, a player wins if the two dice show the same number, focusing on identical results from independent random events. In the second variant, success is defined by the sum of the dice being an even number, which relies on the sum's divisibility. This shift in evaluation criteria increases win chances, as there are more combinations for even sums. Thus, player success is influenced more by the total result rather than specific matching values .

Boolean values are used in programming to make decisions through conditional structures. A boolean condition evaluates to either True or False, which determines the execution path of the program. For example, in the dice game, a comparison checks if two numbers are equal, and the result (True or False) dictates which message is printed (winning or losing).

Yes, the condition 'a >= b' can be creatively implemented using logical operations like 'not(a < b)'. By negating the condition 'a < b', it logically translates to 'a' being greater than or equal to 'b'. Such alternative representations maintain the same logical meaning and can be useful in contexts where certain operations or functions may not directly support all relational operators .

The modulo operator is necessary because it checks if a number is divisible by another without a remainder. In this case, using 's % 2 == 0' verifies if the sum is even, as even numbers are divisible by 2 without remainder. This serves as the condition to determine whether the player wins .

The programming structure used is the 'if-else' conditional statement. It evaluates whether the numbers are equal (x == y). If true, one outcome is executed (winning message); if false, another outcome occurs (losing message). This allows the program to handle multiple outcomes based on specific conditions .

The algorithm aims to determine if two non-zero integers 'a' and 'b' are 'amis' by checking two conditions: first, whether their sum is even, and second, whether one is divisible by the other. If both conditions are satisfied, the numbers are classified as 'amis'; otherwise, they are not .

The outcome of the dice game is directly influenced by the random number generation process because the numbers rolled are determined by random integers between 1 and 6. This randomness is achieved using the 'randint' function from Python's 'random' module. Since each number from 1 to 6 has an equal probability of being chosen, the game's result, which is based on whether the two dice have the same number (or an even sum in a modified version), is entirely dependent on these random values .

The 'randint' function is essential in simulating dice rolls because it generates a pseudo-random integer between two specified values, inclusive. In the dice game, it is used to simulate the roll of a six-sided die by generating a random number between 1 and 6. This function enables the randomness necessary to model the unpredictability of dice rolls in a simple and effective way .

Vous aimerez peut-être aussi