Lesson / Lección 2: For Loops/Repeats

Reeborg Progress
30%

Exercise: Around 1

Guiding Questions:

  • You may have noticed in Newspaper 0 that some of the same code was written over and over again. How do we reduce redundancy?

We can avoid redundancy by using a control statement called a for loop. This will allow you to repeat a certain command or set of commands multiple times without actually typing it out over and over again (making it easier for others to read your code and condensing the length of your code).

There are two ways to do this in Reeborg’s world. You can choose which one is better for you.

Option 1 (in Python language, a bit harder to grasp)

for i in range(5):
    #commands to repeat

Option 2 (only in Reeborg language, easier to grasp)

repeat 5:
    #commands to repeat
  • Note: repeat does not actually exist in Python language, but Reeborg has this function defined already. Also, put the number of times you want to repeat the commands where the 5 is.

If you are having trouble, try writing out the program command by command and then have point out to which parts are the same. Use that warm-up as a starting point to figure out what information goes in the #number of repeats and #commands to repeat fields.

Exercise: Hurdle 1

Guiding Questions:

  • How could you use repeat to jump the hurdles?
  • What commands would you put inside the for loop (what actions need to be repeated)?

This exercise expands on the concept of a for loop. Use the concepts you learned in Around 1 to help you out.


Ejercicio: Around 1

Preguntas orientadoras:

  • Es posible que haya notado en el Newspaper 0 que parte del mismo código fue escrito una y otra vez. ¿Cómo reducimos la redundancia?

Podemos evitar la redundancia utilizando una instrucción for loop. Esto le permitirá repetir un determinado función o conjunto de funciones varias veces sin tener que escribirlo una y otra vez (lo que facilita que otros lean su código y condensen la longitud de su código).

Hay dos formas de hacer esto en el mundo de Reeborgs. Puedes elegir cuál es mejor para ti.

Opción 1 (en lenguaje Python, un poco más difícil de entender)

for i in range(5):
    #funciones para repitir

Opción 2 (solo en Reeborg, más fácil de entender)

repeat 5:
    #funciones para repitir
  • Nota: la repetición en realidad no existe en lenguaje Python, pero Reeborg ya tiene esta función definida. Además, coloque la cantidad de veces que desea repetir los comandos donde está el 5.

Si tiene problemas, intente escribir la función del programa una por una y luego indique qué partes son las mismas. Use ese calentamiento como punto de partida para descubrir qué información va en el # número de repeticiones y #comandos para repetir campos.

Ejercicio: Hurdle 1

Preguntas orientadoras:

  • ¿Cómo podrías usar repetir para saltar los obstáculos?
  • ¿Qué funciones pondrías dentro del for loop (qué acciones deben repetirse)?

Este ejercicio amplía el concepto de un for loop. Usa los conceptos que aprendiste en Around 1 para ayudarte.