Counting loops · Laços de contagem
Repeat with for
- A
forloop repeats code a fixed number of times. - The body of the loop is indented, like an
if. - A loop variable holds a new value on each pass.
Repetir com for
- Um loop
forrepete código um número fixo de vezes. - O corpo do loop está recuado, assim como um
if. - Uma variável de loop segura um novo valor em cada passagem.
Counting with range
range(5)gives the numbers0, 1, 2, 3, 4(stops before 5).range(1, 6)gives1, 2, 3, 4, 5(start included, stop not).range(0, 10, 2)counts in steps of 2.
Contando com range
range(5)gera os números0, 1, 2, 3, 4(para antes de 5).range(1, 6)gera1, 2, 3, 4, 5(início incluído, fim não).range(0, 10, 2)conta em passos de 2.
for i in range(5):
print(i)
Build a total (accumulator)
- An accumulator is a variable that grows inside a loop.
- Start it at
0before the loop. - Add to it on each pass to build a running total.
Construir um total (acumulador)
- Um acumulador é uma variável que cresce dentro de um loop.
- Inicie-o em
0antes do loop. - Some a cada passagem para construir um total acumulado.
total = 0
for n in range(1, 6):
total = total + n
print(total)
In Cambridge pseudocode
- A Python
for … rangeloop is the exam's count-controlledFOR … NEXT.
Em pseudocódigo do Cambridge
- Um loop Python
for … rangeé oFOR … NEXTcontrolado por contagem do exame.
Total ← 0
FOR I ← 1 TO 5
Total ← Total + I
NEXT I
OUTPUT Total
Words and a number on one line
- A comma inside
printputs a space between the parts. print("7 x", 1, "=", 7 * 1)prints7 x 1 = 7.+cannot join a number to text. Use the comma, or turn the number into text withstr.
Palavras e um número em uma linha
- Uma vírgula dentro de
printcoloca um espaço entre as partes. print("7 x", 1, "=", 7 * 1)imprime7 x 1 = 7.+não pode juntar um número a um texto. Use a vírgula, ou transforme o número em texto comstr.
Common mistakes
range(n)counts0ton - 1, not1ton.range(a, b)stops beforeb.- The loop body must be indented under the
forline.
Erros comuns
range(n)conta de0an - 1, não de1an.range(a, b)para antes deb.- O corpo do loop deve estar recuado sob a linha do
for.
Now you try
- Use a
forloop withrange(...)in each task. - Press Check answer to test your code.
Agora você tenta
- Use um loop
forcomrange(...)em cada tarefa. - Clique em Check answer para testar seu código.
A counting loop, pass by pass · Um laço de contagem, passo a passo
A for loop repeats its body; watch the counter and the running total · total acumulado. · Um laço for repete seu corpo; observe o contador e o total acumulado.
Use a for loop to print the numbers 1 to · até 5, each on its own line. · Use um laço for para imprimir os números 1 até 5, cada um em sua própria linha.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Use a loop and an accumulator to add up every whole number from 1 to · até 100. Store the total in total. · Use um laço e um acumulador para somar todos os números inteiros de 1 a 100. Armazene o total em total.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Multiply together every whole number from 1 to · até 5 (that is 1×2×3×4×5). Start product at 1 and store the result there. · Multiplique juntos todos os números inteiros de 1 a 5 (isto é 1×2×3×4×5). Inicie product em 1 e armazene o resultado lá.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Print exactly one line: 7 x 1 = 7. Finish the print that is already started. A comma between the parts prints them with spaces: print("7 x", 1, "=", 7 * 1). · Imprima exatamente uma linha: 7 x 1 = 7. Finalize o print que já foi iniciado. Uma vírgula entre as partes os imprime com espaços: print("7 x", 1, "=", 7 * 1).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Use a for loop to print the first five lines of the 7 times table: 7 x 1 = 7, 7 x 2 = 14, up to 7 x 5 = 35. The print from the last task is already inside the loop — change the 1 into the loop variable. · Use um laço for para imprimir as primeiras cinco linhas da tabela de multiplicar por 7: 7 x 1 = 7, 7 x 2 = 14, até 7 x 5 = 35. O print da tarefa anterior já está dentro do laço — mude o 1 para a variável do laço.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.