Skip to content
HackoryWEB SECURITY ACADEMYProgramming

Unit 08 · Bucles: for y while · Lesson 04

Repetir con range()

range() da una serie de números para recorrer con for.

Lesson content is in Spanish.

Python in your browser

The first time, Python downloads about 5 MB (only once; then it stays saved). If you can, use Wi-Fi.

Python is already saved in this browser: it will not download again.

Python downloads when you start reading.
  1. 01Understand
  2. 02Try
  3. 03Explain

The situation

Una agenda confirma turnos numerados: turno 1, turno 2, turno 3. La cantidad de turnos cambia cada día. Hace falta repetir algo una cantidad de veces, con el número de cada vuelta.

Understand each piece

  • range(1, 4)

    Da los números 1, 2 y 3. Empieza en el primero y termina antes del segundo: el 4 no se incluye.

  • for numero in range(...):

    Recorre esos números como si fueran una lista.

  • range(1, n + 1)

    Para llegar hasta n incluido, el segundo número tiene que ser n + 1. Si n es 0, range(1, 1) no da ningún número.

Example

for numero in range(1, 4):
    print("Paso", numero)

range(1, 4) da 1, 2 y 3. El bloque se repite tres veces y muestra Paso 1, Paso 2 y Paso 3.

Before moving on: what will happen?

¿Qué va a mostrar este programa?
for i in range(1, 3):
    print(i)

Your mission

Confirmá los turnos

La prueba crea la variable turnos con la cantidad de turnos del día (puede ser 0). Mostrá una línea por turno, numerados desde 1, y al final Agenda lista. Con turnos = 3, la salida es: Turno 1: confirmado Turno 2: confirmado Turno 3: confirmado Agenda lista.

  1. for numero in range(1, turnos + 1):
  2. Adentro: print(f"Turno {numero}: confirmado").
  3. Después del for: print("Agenda lista.").

If you get stuck

One hint at a time: the first points the way and the last almost solves it.

The key idea

range(inicio, fin) da los números desde inicio hasta antes de fin.

Solve with help

If you have already tried, you can see a reference solution. The lesson is marked as solved with help and adds no XP.