Totalling & counting · 합계 및 세기
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.
합계 계산: 누적 합계(Running Total)
- 합계 계산은 일련의 숫자를 모두 더합니다.
total을 0으로 초기화한 후, 매번 변수에 값을 더합니다.- 이 패턴
total = total + value을 accumulator(누적기) 라고 합니다.
nums = [10, 20, 30]
total = 0
for n in nums:
total = total + n
print(total) # 60
Building a running total · 누적 합계 만들기
Totalling — start at 0, then add each value as the loop visits it. · 합계 — 0으로 시작하여 루프가 각 값을 방문할 때마다 더합니다.
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.
카운팅: 누적 카운트(Running Count)
- 카운팅은 주어진 규칙을 만족하는 값의 개수를 세어냅니다.
count를 0에서 시작하여 규칙이 참일 때마다 1을 추가합니다.
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.
평균 = 합계 ÷ 개수
- 평균은 모든 값의 합을 값의 개수로 나눈 것입니다.
len(nums)는 목록에 포함된 항목의 수를 반환합니다.
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.
시험: 표준 방법
- 합계 계산과 카운팅은 교재에 명시된 표준 방법입니다.
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.
흔한 실수
- 루프 실행 전에 합계를
0로 설정한 후, 루프 내부에서 더합니다. - 카운터는
0에서 시작하여 매번1씩 증가합니다. - 루프 내부에서 합계를 초기화하지 마십시오.
Now you try
- Use a loop with a running total or count.
- Press Check answer to test your code.
이제 직접 해보기
- 누적 합계 또는 카운터를 사용하는 루프를 적용하십시오.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
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). · 리스트 nums이 미리 설정되어 있습니다. 루프를 사용하여 모든 숫자의 합을 구하고 결과를 변수 total에 저장하세요 (정답은 18).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Count how many numbers in nums are greater than 0. Store the count in count (the answer is 2). · 리스트 nums에서 0보다 큰 숫자가 몇 개 있는지 세어, 그 개수를 변수 count에 저장하세요 (정답은 2).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
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). · 리스트 nums의 숫자들의 평균(총합 ÷ 개수)을 계산하여 변수 average에 저장하세요 (정답은 4.0).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.