Lesson / Lección 4: While Loops

Reeborg Progress
60%

Exercise: Around 1 – variable

This map may look familiar, but there is one difference: The size of the world changes! Each time you run your code, the size of the world will change to a random size.

Guiding Questions:

  • Would a for loop work here? Why or why not?
  • If we don’t know how large the world is, what type of condition would we need to check for to know when to stop Reeborg?
  • How can we use the tokens that Reeborg has to keep track of where Reeborg started?
  • How can we use the presence of this token at the start to get Reeborg to get back to the start position?

Introducing the “while” loop

The while loop is a relative of the for loop and the if statement. Essentially, it says to repeat while some condition is true.

The while loop would be used in the following way:

while some_condition:
    #commands to execute

Think of a time that you may have used a while loop in your life. For example, while I’m hungry, I’ll keep eating or while I’m not at my destination, I’m going to keep driving. While the condition is true, we will keep doing the assigned action.

The conditions to check for that might be useful include:

  • front_is_clear()
  • wall_in_front()
  • object_here()

Another important aspect of loops is the not condition, which can go in front of a condition to negate what the condition checks for.

Exercise: Hurdle 2

In this exercise, the location of the flag changes.

Guiding Questions:

  • How is this problem similar and different from Hurdle 1?
  • How could you modify your Hurdle 1 code to solve this problem?

Once you’ve completed Hurdle 2, go back to Hurdle 1 and see if you code also solves it.


Ejercicio: Around 1 – variable

Este mapa puede parecer familiar, pero hay una diferencia: ¡el tamaño del mundo cambia! Cada vez que ejecuta su código, el tamaño del mundo cambiará a un tamaño aleatorio.

Preguntas orientadoras:

  • ¿Funcionaría un for loop aquí? ¿Por qué o por qué no?
  • Si no sabemos qué tan grande es el mundo, ¿qué tipo de condición necesitaríamos verificar para saber cuándo detener Reeborg?
  • ¿Cómo podemos usar los tokens que Reeborg tiene para hacer un seguimiento de dónde comenzó Reeborg?
  • ¿Cómo podemos usar la presencia de este token al comienzo para que Reeborg vuelva a la posición de inicio?

Presentamos el ciclo “while”

El ciclo while es un pariente del ciclo for y la instrucción if. Esencialmente, dice repetir mientras alguna condición es verdadera.

El bucle while se usaría de la siguiente manera:

while algun_condition:
    #funciones para ejecutar

Piense en un momento en el que puede haber utilizado un ciclo while en su vida. Por ejemplo, mientras tenga hambre, seguiré comiendo o mientras no esté en mi destino, seguiré conduciendo. Si bien la condición es verdadera, seguiremos haciendo la acción asignada.

Las condiciones para verificar que podrían ser útiles incluyen:

  • front_is_clear()
  • wall_in_front()
  • object_here()

Otro aspecto importante de los ciclos es la no condición, que puede ir delante de una condición para negar lo que la condición verifica.

Ejercicio: Hurdle 2

Preguntas orientadoras:

  • ¿Cómo es este problema similar y diferente de Hurdle 1?
  • ¿Cómo podría modificar su código Hurdle 1 para resolver este problema?

Una vez que haya completado Hurdle 2, regrese a Hurdle 1 y vea si su código también lo resuelve.