Boucles while

Introduction à Python pour les développeurs

Jasmin Ludolf

Senior Data Science Content Developer

Instruction If

Instruction If

Flow of an if statement: start > condition met > perform action > exit

Introduction à Python pour les développeurs

Instruction if versus boucle while

Instruction If

Flow of an if statement: start > condition met > perform action > exit

Boucle while

Flow of a while loop: start > condition met > perform action > loop > repeat until condition is no longer met

Introduction à Python pour les développeurs

Boucle while

while condition:
    action
  • Toute tâche continue
    • Accélérer while qu'un bouton est enfoncé

Games console with a racing game

1 https://unsplash.com/@joaoscferrao
Introduction à Python pour les développeurs

Boucle while

ingredients_to_add = 5
items_added = 0
# Keep adding while we have items left
while items_added < ingredients_to_add:

items_added += 1 remaining = ingredients_to_add - items_added print(remaining, "ingredients left to add")
Introduction à Python pour les développeurs

Résultat

4 ingredients left to add
3 ingredients left to add
2 ingredients left to add
1 ingredients left to add
0 ingredients left to add
  • La boucle se termine lorsque items_added est égal à ingredients_to_add
Introduction à Python pour les développeurs

Une mise en garde

  • while s'exécute en continu tant que la condition est remplie
ingredients_to_add = 5
items_added = 0

while items_added < ingredients_to_add:
    remaining = ingredients_to_add - items_added
    print(remaining, "ingredients left")
Introduction à Python pour les développeurs

Exécution infinie

ingredients_to_add = 5
items_added = 0

# INFINITE LOOP - never exits!
while items_added < ingredients_to_add:
    remaining = ingredients_to_add - items_added
    print(remaining, "ingredients left")
    # Forgot to increment items_added!
  • La condition ne devient jamais False
  • La boucle s'exécute indéfiniment, le programme se bloque
  • Erreur courante chez les développeurs
Introduction à Python pour les développeurs

Interrompre une boucle

while items_added < ingredients_to_add:
    remaining = ingredients_to_add - items_added
    print(remaining, "ingredients left")

# Terminate the loop break
  • break peut également être utilisé dans les boucles for

  • Si le code est déjà en cours d'exécution : Control + C / Command + C

Introduction à Python pour les développeurs

Instructions conditionnelles dans les boucles while

ingredients_to_add = 5
items_added = 0


while items_added < ingredients_to_add: items_added += 1 remaining = ingredients_to_add - items_added
if remaining > 3: print("Several ingredients remaining")
elif remaining >= 1: print("Almost done!")
else: print("Shopping list complete!")
Introduction à Python pour les développeurs

Résultat des instructions conditionnelles

Several ingredients remaining
Several ingredients remaining
Almost done!
Almost done!
Shopping list complete!
Introduction à Python pour les développeurs

Passons à la pratique !

Introduction à Python pour les développeurs

Preparing Video For Download...