Comment arrêter boucle for en python
RésoluClaraLau25 Messages postés 4 Date d'inscription Statut Membre Dernière intervention -
Bonjour, je souhaiterai arrêter ma boucle for pour stopper mon animation. J'ai essayé avec break mais cela ne fonctionne pas sur mon programme. Pouvez-vous m'aider ?
programme :
for g in range (4): pg2=np.zeros([l,c]) for i in range(l): for j in range (c): nbv=nb_voisins(pg,i,j) if pg[1,3]==1 and pg[2,3]==1 and (nbv>=1): pg2[i,j]=1 #arrondissement ayant des habitants malades pg2[0,3]=0 #arrondissement mer pg2[0,0]=0 #arrondissment sans habitant pg2[2,1]=0 #arrondissement mer pg2[1,1]=0 #arrondissement mer pg2[0,1]=0 #arrondissement mer affichage (pg2) pg=pg2
Macintosh / Safari 15.2
- Python stopper une boucle
- Citizen code python avis - Accueil - Outils
- Stopper pub youtube - Accueil - Streaming
- Mon pc s'allume et s'éteint en boucle ✓ - Forum Matériel & Système
- Python est introuvable. exúcutez sans argument pour procúder ó l - Forum Python
- Python pix ✓ - Forum Python
2 réponses
Bonjour,
D'abord il doit y avoir la logique de ton programme. Est-ce qu'avec les paramètres il existe une situation où il s'arrête seul ?
Après break fonctionne. Mais seulement pour un niveau. Où voulais-tu le placer ? Il n'y a pas de break pour n niveaux imbriqués.
Il faut alors pratiquer autrement. Comme mettre tout ton code dans une fonction, et remplacer break par return. Ou gérer un flag avec une boucle while.
def maFonction(): for g in range (4): pg2=np.zeros([l,c]) for i in range(l): for j in range (c): nbv=nb_voisins(pg,i,j) if pg[1,3]==1 and pg[2,3]==1 and (nbv>=1): pg2[i,j]=1 #arrondissement ayant des habitants malades pg2[0,3]=0 #arrondissement mer pg2[0,0]=0 #arrondissment sans habitant pg2[2,1]=0 #arrondissement mer pg2[1,1]=0 #arrondissement mer pg2[0,1]=0 #arrondissement mer if ...... return affichage (pg2) pg=pg2 flag = True while flag: for g in range (4): pg2=np.zeros([l,c]) for i in range(l): for j in range (c): nbv=nb_voisins(pg,i,j) if pg[1,3]==1 and pg[2,3]==1 and (nbv>=1): pg2[i,j]=1 #arrondissement ayant des habitants malades pg2[0,3]=0 #arrondissement mer pg2[0,0]=0 #arrondissment sans habitant pg2[2,1]=0 #arrondissement mer pg2[1,1]=0 #arrondissement mer pg2[0,1]=0 #arrondissement mer if ...... flag = False affichage (pg2) pg=pg2
PS : pour afficher du code, il faut utiliser l’icône spécialisée <>, et donner le langage, Python, pour la coloration syntaxique.
Salut, une manière très élégante de sortir de boucles imbriquées est d'utiliser le else du for (ou while).
Exemple simple :
.
for i in range(10): print(i) for j in range(10): if i * j == 21: print('ok', i, j) break else: continue break
.
On peut le faire autant de fois qu'il n'y a de profondeurs.
.
for i in range(10): for j in range(10): print(i, j) for k in range(10): if i * j * k == 105: print('ok', i, j, k) break else: continue break else: continue break
Bonjour, merci pour votre retour rapide
Effectivement il existe un paramètre ou il s'arrête seul : quand toutes les cases sont égales à 1.
Pour break j'ai voulu le placer dans la boucle, c'est surement pour cela que ça ne marchait pas.
J'ai essayé avec les méthodes que vous m'avez donnés mais je n'y arrive toujours pas, il doit y avoir un problème dans mon raisonnement ..
l=5
c=4
pg=initialisation_arrondissement(l,c)
affichage(pg)
def maFonction():
for g in range (4):
pg2=np.zeros([l,c])
for i in range(l):
for j in range (c):
nbv=nb_voisins(pg,i,j)
if pg[1,3]==1 and pg[2,3]==1 and (nbv>=1):
pg2[i,j]=1 #arrondissement ayant des habitants malades
pg2[0,3]=0 #arrondissement mer
pg2[0,0]=0 #arrondissment sans habitant
pg2[2,1]=0 #arrondissement mer
pg2[1,1]=0 #arrondissement mer
pg2[0,1]=0 #arrondissement mer
if pg2[i,j]==1 and pg2[0,3]==0 and pg2[0,0]==0 and pg2[2,1]==0 and pg2[1,1]==0 and pg2[0,1]==0:
return
affichage (pg2)
pg=pg2
Il te manque juste l'appel de la fonction ;-) la déclaration d'une fonction ne l'exécute pas. Et surtout pour une présentation claire, on ne déclare pas une fonction au milieu du corps principal du programme, mais tout au début.
PS BIS : pour afficher du code, il faut utiliser l’icône spécialisée <>, et donner le langage, Python, pour la coloration syntaxique.
Merci j'ai trouvé comment faire :-)
Bonne journée !