Sintaxis de la sentencia if
Revisemos la construcción de la sentencia if y consideremos otro ejemplo.
123age = 18 if age == 18: print('Adult')
Hablemos de la sintaxis.
- La palabra clave utilizada para el operador condicional es
if. Es esencial notar que es sensible a mayúsculas, por lo tanto, usarIfcon una I mayúscula resultará en un error ya que es incorrecto; - Después de la palabra clave
if, nos encontramos con una condición. Vale la pena mencionar que las condiciones se pueden expresar de diversas maneras, un tema que profundizaremos más adelante. En nuestro ejemplo, estamos comprobando si una variable es igual a un valor específico, y para hacer esto, empleamos el operador de igualdad; - Luego, después de la condición, colocamos dos puntos;
- Después, tenemos una indentación de código que se ejecuta dentro del bloque if.
Es decir, esta es una instrucción que se ejecuta si nuestras condiciones son True.
Example 2: When Nothing Executed
12345steps_taken = 10000 step_goal = 10000 if steps_taken < step_goal: print(f"Keep going! You need {step_goal - steps_taken} more steps to reach your goal.")
In this case, the condition steps_taken < step_goal evaluates to False because steps_taken is equal to step_goal. Since the condition is not met, the code block inside the if statement is not executed, and nothing is printed to the console. This demonstrates that the code only runs when the condition evaluates to True.
The image depicts the flow of an if statement:
- Condition Check: the program evaluates whether the condition is
TrueorFalse; - Execution: if the condition is
True, the indented code block runs. Otherwise, the program skips it.
Swipe to start coding
Your Fitness Tracker needs to do more than just check steps! This time, it will motivate users to complete their workout by comparing their calories burned against daily goals.
Fill in the blanks in the code you've already been given.
Once you've completed this task, click the button below the code to check your solution.
Solución
¡Gracias por tus comentarios!
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Resumir este capítulo
Explicar el código en file
Explicar por qué file no resuelve la tarea
Awesome!
Completion rate improved to 5.88
Sintaxis de la sentencia if
Desliza para mostrar el menú
Revisemos la construcción de la sentencia if y consideremos otro ejemplo.
123age = 18 if age == 18: print('Adult')
Hablemos de la sintaxis.
- La palabra clave utilizada para el operador condicional es
if. Es esencial notar que es sensible a mayúsculas, por lo tanto, usarIfcon una I mayúscula resultará en un error ya que es incorrecto; - Después de la palabra clave
if, nos encontramos con una condición. Vale la pena mencionar que las condiciones se pueden expresar de diversas maneras, un tema que profundizaremos más adelante. En nuestro ejemplo, estamos comprobando si una variable es igual a un valor específico, y para hacer esto, empleamos el operador de igualdad; - Luego, después de la condición, colocamos dos puntos;
- Después, tenemos una indentación de código que se ejecuta dentro del bloque if.
Es decir, esta es una instrucción que se ejecuta si nuestras condiciones son True.
Example 2: When Nothing Executed
12345steps_taken = 10000 step_goal = 10000 if steps_taken < step_goal: print(f"Keep going! You need {step_goal - steps_taken} more steps to reach your goal.")
In this case, the condition steps_taken < step_goal evaluates to False because steps_taken is equal to step_goal. Since the condition is not met, the code block inside the if statement is not executed, and nothing is printed to the console. This demonstrates that the code only runs when the condition evaluates to True.
The image depicts the flow of an if statement:
- Condition Check: the program evaluates whether the condition is
TrueorFalse; - Execution: if the condition is
True, the indented code block runs. Otherwise, the program skips it.
Swipe to start coding
Your Fitness Tracker needs to do more than just check steps! This time, it will motivate users to complete their workout by comparing their calories burned against daily goals.
Fill in the blanks in the code you've already been given.
Once you've completed this task, click the button below the code to check your solution.
Solución
¡Gracias por tus comentarios!
single