DEV Community

Cover image for Ibuprofeno.py💊| #165: Explica este código Python
Cristian Fernando
Cristian Fernando

Posted on

Ibuprofeno.py💊| #165: Explica este código Python

Explica este código Python

Dificultad: Fácil

i = 0
while i < 3:
    print(i)
    i += 1
else:
    print(0)
Enter fullscreen mode Exit fullscreen mode
  • A. 0 1 2 3 0
  • B. 0 1 2 0
  • C. 0 1 2
  • D. Ninguna de las anteriores

Respuesta:

👉 B. 0 1 2 0

En Python existe la peculiaridad de que podemos usar la clausula else asociada a un bucle while.

El funcionamiento es el siguiente: el bucle while funcionará de manera habitual y normal hasta que se presente el primer caso donde la condición no se cumpla, entonces una vez pasado esto saltamos automáticamente al else.

En nuestro ejemplo el while imprimirá 0 1 2, cuando i = 3 la condición i < 3 ya no se cumple, entonces salta al bucle else que imprime un valor en duro print(0) teniendo 0 1 2 0.

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay