Lesson / Lección 5: Combining Conditions/Advanced Conditions

Reeborg Progress
75%

Exercise: Hurdle 3

In this exercise, the position and number of hurdles changes.

Guiding Questions:

  • How can you combine/nest while loops and if statements together to solve this problem?
  • What types of things do you need to check for?
  • How can we decompose the code into smaller functions to make it more organized?

There are a lot of hurdles that we have to jump through, maybe we can set a function to “jump” for us. maybe calling it `jump_hurdle`?

Introduction to If/Else Statements

We learned about if statements in Section 4. There are a few more things to if statements than we previously discussed. We don’t only think in only if statements as humans, for example going back to the traffic lights example. We don’t think like this:

  • If the light is green, then go
  • If the light is red, then stop

We actually think of it like this:

  • If the light is green, then go
  • Else, stop.

Here we only check if the light is green, if the color isn’t green then we stop. That way we don’t have to check each color of the traffic light but rather one color.

The syntax for if/else statements:

  if some_condition:
    #commands
  else:
    #other commands

If you see, you don’t need a condition for the else statement, that’s because if the if statement does not run, the computer will then run the else commands.

Exercise: Hurdle 4

In this exercise, the position, height and number of hurdles changes.

Guiding Questions:

  • How can you alter Hurdle 3 to solve this problem?

If your code from Hurdle 3 is well-decomposed, this problem should be a small extension on Hurdle 3. Think about how while and if can work together and think about adding some new functions.

If you are struggling, jump back to 3. Basic Conditions/If Statements and 4. While Loops and review. It may be helpful to try Around 2, 3 and 4 exercises.

Test your code from Hurdle 4 on Hurdle 1, Hurdle 2 and Hurdle 3. Why do you think it works for such a diverse set of problems?

Congratulations!

Congratulations on finishing the StreetCode Reeborg sequence! The following section goes over some challenge problems if you want to learn even more and challenge yourself. You can also try any other problems you’re interested in.


Ejercicio: Hurdle 3

En este ejercicio, la posición y el número de obstáculos cambian.

Preguntas orientadoras:

  • ¿Cómo se pueden combinar / anidar los ciclos while y las declaraciones if para resolver este problema?
  • ¿Qué tipo de cosas necesita verificar?
  • ¿Cómo podemos descomponer el código en funciones más pequeñas para hacerlo más organizado?

Hay muchos obstáculos por los que tenemos que pasar, tal vez podamos establecer una función para “saltar” para nosotros, tal vez llamándolo obstáculo_de_salto?

Introducción a las declaraciones If / Else

Aprendimos acerca de las declaraciones if en la Sección 4. Hay algunas cosas más que las declaraciones if de las que discutimos anteriormente. No solo pensamos en si las declaraciones como humanos, por ejemplo, volviendo al ejemplo de los semáforos. No pensamos así:

  • Si la luz es verde, entonces ve
  • Si la luz es roja, entonces pare

En realidad la pensamos así:

  • Si la luz es verde, entonces ve
  • De lo contrario, para.

Aquí solo verificamos si la luz es verde, si el color no es verde, entonces nos detenemos. De esa manera, no tenemos que verificar cada color del semáforo, sino un color.

La sintaxis de las declaraciones if / else:

  if algun_condition:
    #funciones
  else:
    #otras funciones

Si ve, no necesita una condición para la instrucción else, porque si la instrucción if no se ejecuta, la computadora ejecutará los comandos else.

Ejercicio: Hurdle 4

En este ejercicio, la posición, la altura y el número de obstáculos cambian.

Preguntas orientadoras:

  • ¿Cómo puedes alterar Hurdle 3 para resolver este problema?

Si su código de Hurdle 3 está bien descompuesto, este problema debería ser una pequeña extensión en Hurdle 3. Piense en cómo y si pueden trabajar juntos la declaracion if y while y piense en agregar algunas funciones nuevas.

Si tiene dificultades, vuelva a 3. Condiciones básicas / Declaraciones If y 4. Ciclos While. Puede ser útil probar los ejercicios Around 2, 3 y 4.

Pruebe su código de Hurdle 4 en Hurdle 1, Hurdle 2 y Hurdle 3. ¿Por qué cree que funciona para un conjunto tan diverso de problemas?

¡Felicidades!

¡Felicitaciones por terminar la secuencia StreetCode Reeborg! La siguiente sección repasa algunos problemas de desafío si desea aprender aún más y desafiarse a sí mismo. También puede probar cualquier otro problema que le interese.