Lesson / Lección 3: Basic Conditions/If Statements

Reeborg Progress
45%

3. Basic Conditions/If Statements

Exercise: Around 1 – apple

Guiding Questions:

  • How could we use an if statement to pick up an apple if there is one where Reeborg is standing, but not when there isn’t one?
  • Knowing that the apples randomize each time, would we be able to write a solution that works on all worlds with what we know so far?

Any solution of just raw commands wouldn’t work if even one apple changed positions in the world, so it couldn’t be used to solve a slightly different problem of Reeborg collecting apples in a world where the apples are arranged in another order.

Introducing the “if” statement

Think about actions that only occur if something is true. For example, when you or your parents are driving, they continue driving if the traffic light is green and they would stop if the traffic light is red. With “if” statements we only execute the command if the statement is true.

if the traffic light is green then continue driving

if some_condition():
    command()

Look through the Reeborg’s keyboard section in the toolbar. Find out which condition might be useful in this exercise where we have to check if an apple is underneath us.

Now, use the for loop code (similar to Around 1) along with if statements to make Reeborg go around and collect all of the apples.


3. Condiciones Básicas / If Statements

Ejercicio: Around 1 – apple

Preguntas orientadoras:

  • ¿Cómo podríamos usar una declaración if para recoger una manzana si hay una en la que Reeborg está parado, pero no cuando no hay una?
  • Sabiendo que las manzanas se aleatorizan cada vez, ¿podríamos escribir una solución que funcione en todos los mundos con lo que sabemos hasta ahora?

Cualquier solución de funciones sin procesar no funcionaría si incluso una manzana cambiara de posición en el mundo, por lo que no podría usarse para resolver un problema ligeramente diferente de Reeborg recolectando manzanas en un mundo donde las manzanas están dispuestas en otro orden.

Presentando la declaración “if”

Piensa en acciones que solo ocurren si algo es cierto. Por ejemplo, cuando usted o sus padres conducen, continúan conduciendo si el semáforo está en verde y se detendrían si el semáforo está en rojo. Con las declaraciones “if” solo ejecutamos el comando si la declaración es verdadera.

Si el semáforo está verde, continúe conduciendo.

if algun_condicion():
    funcion()

Mire a través de la sección del teclado de Reeborg en la barra de herramientas. Averigüe qué condición podría ser útil en este ejercicio donde tenemos que verificar si hay una manzana debajo de nosotros.

Ahora, use el código de for loop (similar a Around 1) junto con las declaraciones if para que Reeborg dé la vuelta y recoja todas las manzanas.