얼음 두께 탐구
이 단계에서는 for 루프를 사용하여 얼음층 두께 측정값 목록을 처리하는 방법을 배우게 됩니다. 코어 샘플의 서로 다른 층에서 얼음의 두께를 나타내는 목록이 주어졌다고 상상해 보세요. 이 층들의 평균 두께를 계산해 보겠습니다.
코딩하기 전에 작업 디렉토리가 /home/labex/project에 있는지 확인하십시오.
이제 ice_thickness.py를 열고 평균 두께를 계산하기 위해 다음 Python 코드를 작성하십시오.
## ice_thickness.py
## Sample data representing ice layer thicknesses in millimeters
ice_layers = [120, 150, 90, 200, 180, 75, 140]
## Initialize the total thickness to 0
total_thickness = 0
## Use a for loop to sum up the thicknesses
for layer in ice_layers:
total_thickness += layer
## Calculate the average thickness
average_thickness = total_thickness / len(ice_layers)
## Print out the average thickness
print(f"The average ice layer thickness is {average_thickness} millimeters.")
코드를 실행하려면 터미널에서 다음 명령을 실행하십시오.
python ice_thickness.py
결과로 평균 얼음층 두께가 출력되는 것을 볼 수 있습니다.
The average ice layer thickness is 136.42857142857142 millimeters.