Contenido del Curso
Tutorial de Bucles en Python
Tutorial de Bucles en Python
La Declaración Else en un Bucle While
En Python, el bloque else
se puede agregar a un bucle while
. El bloque else
se ejecuta cuando el bucle termina normalmente, lo que significa que la condición del bucle se convierte en False
sin encontrar una declaración break
.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize index i = 0 # Iterate through the destinations while i < len(travel_list): print(travel_list[i]) i += 1 else: print("All destinations have been listed!")
Ejemplo: Rompiendo el Bucle
Si el bucle termina con una declaración break
(por ejemplo, cuando se encuentra una ciudad específica), el bloque else
no se ejecuta.
# List of travel destinations travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize index i = 0 # Search for a specific destination while i < len(travel_list): if travel_list[i] == "Barcelona": break print(travel_list[i]) i += 1 else: print("All destinations have been listed!") # This won't execute if break is triggered.
Here, the loop stops as soon as it finds "Barcelona"
. The else
block is skipped because the loop does not terminate normally but is interrupted by the break
statement.
Swipe to show code editor
Imagine planning your travel within a fixed budget. This program dynamically calculates and prints the cost of each trip as long as your budget allows. Once the budget is exhausted, the else
statement provides a closing message confirming that all affordable trips have been planned.
This program demonstrates how to use the else
block with a while
loop effectively.
Expected Output:
Solución
¡Gracias por tus comentarios!
La Declaración Else en un Bucle While
En Python, el bloque else
se puede agregar a un bucle while
. El bloque else
se ejecuta cuando el bucle termina normalmente, lo que significa que la condición del bucle se convierte en False
sin encontrar una declaración break
.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize index i = 0 # Iterate through the destinations while i < len(travel_list): print(travel_list[i]) i += 1 else: print("All destinations have been listed!")
Ejemplo: Rompiendo el Bucle
Si el bucle termina con una declaración break
(por ejemplo, cuando se encuentra una ciudad específica), el bloque else
no se ejecuta.
# List of travel destinations travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Initialize index i = 0 # Search for a specific destination while i < len(travel_list): if travel_list[i] == "Barcelona": break print(travel_list[i]) i += 1 else: print("All destinations have been listed!") # This won't execute if break is triggered.
Here, the loop stops as soon as it finds "Barcelona"
. The else
block is skipped because the loop does not terminate normally but is interrupted by the break
statement.
Swipe to show code editor
Imagine planning your travel within a fixed budget. This program dynamically calculates and prints the cost of each trip as long as your budget allows. Once the budget is exhausted, the else
statement provides a closing message confirming that all affordable trips have been planned.
This program demonstrates how to use the else
block with a while
loop effectively.
Expected Output:
Solución
¡Gracias por tus comentarios!