Python - 문제 미리보기

문제 1965

hard
다음 코드에서 빈칸에 들어갈 적절한 구문은?
```python
scores = (85, 92, 78, 95, 88)
total = 0
count = 0

_____ score _____ scores:
total += score
count += 1

average = total / count
print(f"평균: {average}")
```
A. `while`, `<`
B. `for`, `in`
C. `if`, `==`
D. `range`, `len`

정답: B



⦁ 튜플의 모든 요소를 순회하면서 합계와 개수를 계산
⦁ `for score in scores`: 구문으로 각 점수에 접근
⦁ 가장 직관적이고 효율적인 방법

완성된 코드:
```python
scores = (85, 92, 78, 95, 88)
total = 0
count = 0

for score in scores:
total += score
count += 1

average = total / count
print(f"평균: {average}")
```
실행 과정:
1. `score = 85`: `total = 85`, `count = 1`
2. `score = 92`: `total = 177`, `count = 2`
3. `score = 78`: `total = 255`, `count = 3`
4. `score = 95`: `total = 350`, `count = 4`
5. `score = 88`: `total = 438`, `count = 5`
6. `average = 438 / 5 = 87.6`

💡 학습 팁

이 문제를 포함한 Python 과목의 모든 문제를 순차적으로 풀어보세요. 진행상황이 자동으로 저장되어 언제든지 이어서 학습할 수 있습니다.