Selection — IF and CASE · Selección — IF y CASE
Making a decision
- Selection means choosing what to do, based on a test.
- An
ifruns its block only when the test is true. - The test gives a Boolean:
TrueorFalse.
Tomar una decisión
- Selección significa elegir qué hacer, basándose en una prueba.
- Un
ifejecuta su bloque solo cuando la prueba es verdadera. - La prueba da un valor booleano:
TrueoFalse.
age = 20
if age >= 18:
print("You may vote")
else: the other path
elseruns when theiftest is false.- Exactly one of the two blocks runs.
else: el otro camino
elsese ejecuta cuando la prueba delifes falsa.- Exactamente uno de los dos bloques se ejecuta.
mark = 45
if mark >= 50:
print("Pass")
else:
print("Fail")
elif: many paths
- Use
elifto test more conditions in order. - Python checks each test top to bottom and stops at the first true one.
elif: muchos caminos
- Usa
elifpara probar más condiciones en orden. - Python revisa cada prueba de arriba hacia abajo y se detiene en la primera que sea verdadera.
mark = 72
if mark >= 70:
print("A")
elif mark >= 50:
print("B")
else:
print("C")
if / elif / else — which runs? · if / elif / else — ¿cuál se ejecuta?
Tests run top-down; the first · primero true one runs, the rest are skipped. · Las pruebas se evalúan de arriba hacia abajo; la primera que sea verdadera se ejecuta, las demás se omiten.
Comparison operators
==means equal to (the exam writes=).!=means not equal to (the exam writes<>).- Also use
<,>,<=,>=.
Operadores de comparación
==significa igual a (el examen escribe=).!=significa diferente de (el examen escribe<>).- También se usan
<,>,<=,>=.
print(5 == 5) # True
print(5 != 3) # True
print(3 <= 2) # False
In the exam: IF and CASE
- Pseudocode uses
IF … THEN … ELSE … ENDIF, orCASEfor many fixed choices.
En el examen: IF y CASE
- El pseudocódigo usa
IF … THEN … ELSE … ENDIF, oCASEpara muchas opciones fijas.
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Common mistakes
- Compare with
==, not=. - Cover every case, including the
else. - Indent the block that belongs to each branch.
Errores comunes
- Compara con
==, no con=. - Cubre todos los casos, incluyendo el
else. - Indenta el bloque que pertenece a cada rama.
Now you try
- Read the input, then choose what to print with
if/elif/else. - Press Check answer to test your code.
Ahora tú intentas
- Lee la entrada, luego elige qué imprimir con
if/elif/else. - Presiona Check answer para probar tu código.
Read a whole number, then print even if it divides by 2 with no remainder, otherwise print odd. For input 4, print even. · Lee un número entero e imprime even si es divisible por 2 con residuo cero, de lo contrario imprime odd. Para la entrada 4, imprime even.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Read a mark (0–100). Print A for 70 or more, B for 50–69, and C for anything less. For input 65, print B. · Lee una calificación (0–100). Imprime A para 70 o más, B para 50–69, y C para cualquier valor menor. Para la entrada 65, imprime B.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Two numbers a and · y b are set for you. Use an if to store the larger · mayor one in biggest. · Se te asignan dos números a y b. Usa un if para almacenar el mayor en biggest.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.