Selection · Selección
Programs make choices
- Selection means the program picks what to do based on a condition.
- A condition is a question that is
TrueorFalse. - This is a key idea in AP CSP: code that decides.
Los programas toman decisiones
- Selección significa que el programa elige qué hacer basándose en una condición.
- Una condición es una pregunta que es
TrueoFalse. - Esta es una idea clave en AP CSP: código que decide.
if / elif / else
ifruns its block only when the condition isTrue.elif("else if") checks another condition if the ones above wereFalse.elseruns when none of the conditions wereTrue.
if / elif / else
ifejecuta su bloque solo cuando la condición esTrue.elif("sino si") verifica otra condición si las anteriores fueronFalse.elsese ejecuta cuando ninguna de las condiciones fueTrue.
hour = 14
if hour < 12:
print("Morning")
elif hour < 18:
print("Afternoon")
else:
print("Evening")
Comparisons make conditions
==equal,!=not equal,<><=>=.==tests if two values are equal; a single=stores a value.- The result of a comparison is
TrueorFalse.
Las comparaciones crean condiciones
==igual,!=no igual,<><=>=.==prueba si dos valores son iguales; un único=almacena un valor.- El resultado de una comparación es
TrueoFalse.
score = 72
print(score >= 50)
print(score == 100)
Boolean logic: and / or / not
andisTrueonly when both sides areTrue.orisTruewhen at least one side isTrue.notflips it:not TrueisFalse.
Lógica booleana: and / or / not
andesTruesolo cuando ambos lados sonTrue.oresTruecuando al menos uno de los lados esTrue.notlo invierte:not TrueesFalse.
age = 16
if age >= 13 and age <= 19:
print("Teenager")
else:
print("Not a teenager")
In AP CSP pseudocode
- Python
if/elsematches CB'sIF(condition){ } ELSE { }. - CB uses words for logic:
AND,OR,NOT. DISPLAY= print,←==.
En pseudocódigo de AP CSP
- Python
if/elsecorresponde alIF(condition){ } ELSE { }de CB. - CB usa palabras para la lógica:
AND,OR,NOT. DISPLAY= print,←==.
IF (score >= 50)
{
DISPLAY "Pass"
}
ELSE
{
DISPLAY "Fail"
}
IF ((age >= 13) AND (age <= 19))
{
DISPLAY "Teenager"
}
Common mistakes
- Compare with
==, not=. - Indent the block that runs when the condition is true.
Errores comunes
- Compare con
==, no con=. - Indente el bloque que se ejecuta cuando la condición es verdadera.
Now you try
- Read each task to see what to print or check.
- Match the output text exactly.
Ahora tú intentas
- Lee cada tarea para ver qué imprimir o verificar.
- Coincide el texto de salida exactamente.
Selection picks a branch · La selección elige una rama
Conditions are tested in order; the first true one runs. · Las condiciones se prueban en orden; la primera verdadera se ejecuta.
Read a score with int(input()). Print A if it is 80 or more, B if it is 60 or more, otherwise C. Use if / elif / else. For input 75, print B. · Lee una puntuación con int(input()). Imprime A si es 80 o más, B si es 60 o más, de lo contrario C. Usa if / elif / else. Para la entrada 75, imprime B.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
temp is 22. Set comfortable to · hasta True when temp is from 18 to · hasta 25 (inclusive), else False. Use one comparison with and. · temp es 22. Asigna a comfortable el valor True cuando temp está entre 18 y 25 (inclusive), de lo contrario False. Usa una comparación con and.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
day is "Sun" and · y is_holiday is False. Print Day off if it is a weekend ("Sat" or "Sun") or a holiday; otherwise print Work day. Use or. · day es "Sun" e is_holiday es False. Imprime Day off si es fin de semana ("Sat" o "Sun") o festivo; de lo contrario imprime Work day. Usa or.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.