Totalling & counting · Sumar y contar
Totalling: a running total
- Totalling adds up a series of numbers.
- Start a
totalat 0, then add each value to it. - The pattern
total = total + valueis called an accumulator.
Acumulación: una suma acumulativa
- Acumular consiste en sumar una serie de números.
- Inicie un
totalen 0, luego sume cada valor a él. - El patrón
total = total + valuese denomina acumulador.
nums = [10, 20, 30]
total = 0
for n in nums:
total = total + n
print(total) # 60
Building a running total · Construir una suma acumulativa
Totalling — start at 0, then add each value as the loop visits it. · Sumar — comienza en 0, luego suma cada valor a medida que el bucle lo visita.
Counting: a running count
- Counting tallies how many values meet a rule.
- Start a
countat 0, then add 1 each time the rule is true.
Conteo: un conteo acumulativo
- Contar determina cuántos valores cumplen con una regla.
- Inicie un
count(contador) en 0, luego sume 1 cada vez que la regla sea verdadera.
nums = [4, -2, 7, -5]
count = 0
for n in nums:
if n > 0:
count = count + 1
print(count) # 2
Average = total ÷ count
- An average is the total divided by how many values there are.
len(nums)gives the number of items in a list.
Promedio = total ÷ count
- Un promedio es el total dividido por la cantidad de valores.
len(nums)devuelve el número de elementos en una lista.
nums = [2, 4, 6]
average = sum(nums) / len(nums)
print(average) # 4.0
In the exam: standard methods
- Totalling and counting are named standard methods in the syllabus.
En el examen: métodos estándar
- Acumular y contar son denominados métodos estándar en el programa de estudios.
Total ← 0
FOR I ← 1 TO 5
Total ← Total + Number[I]
NEXT I
Common mistakes
- Set the total to
0before the loop, then add to it inside. - A counter starts at
0and goes up by1each time. - Do not reset the total inside the loop.
Errores comunes
- Establezca el total en
0antes del bucle, y luego agréguelo dentro. - Un contador comienza en
0y aumenta en1cada vez. - No reinicie el total dentro del bucle.
Now you try
- Use a loop with a running total or count.
- Press Check answer to test your code.
Ahora practique
- Utilice un bucle con una suma o un conteo acumulativo.
- Presione Comprobar respuesta para probar su código.
The list nums is set for you. Use a loop to add up every number and store the result in total (the answer is 18). · La lista nums está configurada para ti. Usa un bucle para sumar todos los números y almacena el resultado en total (la respuesta es 18).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Count how many numbers in nums are greater than 0. Store the count in count (the answer is 2). · Cuenta cuántos números en nums son mayores que 0. Almacena el conteo en count (la respuesta es 2).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Work out the average of the numbers in nums (the total divided by how many there are) and store it in average (the answer is 4.0). · Calcula la media de los números en nums (el total dividido por la cantidad de elementos) y almacénala en average (la respuesta es 4.0).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.