Faire fonctionner un programme "tant que"

Fermé
TheCorsair Messages postés 1 Date d'inscription dimanche 4 octobre 2015 Statut Membre Dernière intervention 4 octobre 2015 - 4 oct. 2015 à 16:24
nidhogg-59 Messages postés 102 Date d'inscription mercredi 20 juin 2012 Statut Membre Dernière intervention 5 décembre 2015 - 9 nov. 2015 à 06:40
Bonjour, je fais un programme sur le 421 et j'ai besoin de le faire tourner tant que le nombre de jetons (noté 'n') n'est pas arriver a 0
auriez-vous une idée ?
actuellement mon programme ne marche pas
merci d'avance !

mon programme:

from random import *
import random;
#Charge
d = (1,2,3,4,5,6)
a = random.choice(d)
b = random.choice(d)
c = random.choice(d)
#
e= [a,b,c]
e.sort(reverse=True)
print (e)
#
d = (1,2,3,4,5,6)
x = random.choice(d)
y = random.choice(d)
z = random.choice(d)
#
f= [x,y,z]
f.sort(reverse=True)
print(f)
#
P1=0
P2=0
#
n=20
if n>0:
    if e==[4,2,1]:
        P1=8
        n=20-8
    else:
        if e[1]==e[2] and e[0]!=e[1]:
            P1=e[0]
            n=20-e[0]
        else:
            if e==[1,1,1]:
                    P1=7
                    n=20-7
            else:
                if e[0]==e[1] and e[0]==e[2]:
                        P1=3
                        n=20-3
                else:
                    if e[0]==e[1]+1 and e[0]==e[2]+2:
                              P1=2
                              n=20-2
                    else:
                        if e[0]==e[1] and e[0]!=e[2] and e[1]!=e[2]:
                                P1=2
                                n=20-2
                        else:
                                 P1=1
                                 n=20-1
    if f==[4,2,1]:
        P2=8
        n=20-8
    else:
        if f[1]==f[2] and f[0]!=f[1]:
            P2=f[0]
            n=20-f[0]
        else:
            if f== [1,1,1]:
                P2=7
                n=20-7
            else:
                if f[0]==f[1] and f[0]==f[2]:
                    P2=3
                    n=20-3
                else:
                    if f[0]==f[1]+1 and f[0]==f[2]+2:
                        P2=2
                        n=20-2
                    else:
                        if f[0]==f[1] and f[0]!=f[2] and f[1]!=f[2]:
                            P2=2
                            n=20-2
                        else:
                            P2=1
                            n=20-1
else:
    if n==0 or n<0:
        print("la partie est finie !")
print("P1=",P1,"P2=",P2,"n=",n)
print("resultat de la Charge:")                        
if P1>P2:
    print("Le joueur 1 a gagné en donnant",P1,"jetons au joueur 2 !")
if P1<P2:
    print("Le joueur 2 a gagné en donnant",P2,"jetons au joueur 1 !")
if P1==P2:
    print("Egalité !")

#Décharge

1 réponse

nidhogg-59 Messages postés 102 Date d'inscription mercredi 20 juin 2012 Statut Membre Dernière intervention 5 décembre 2015 27
9 nov. 2015 à 06:40
Bonjour,
Pour faire boucler un programme tant qu'une condition n'est pas vérifiée, il faut uiliser le mot-clé while:
while condition:
  do_something ()

https://docs.python.org/2/reference/compound_stmts.html#the-while-statement
Cordialement
0