with open('data/chicken.txt','r') as f:
margin = {}
for line in f:
margin_list = line.strip().split(': ')
margin[margin_list[0]] = margin_list[1]
total_margin = 0
avg_margin = 0
cnt = 0
for i in margin.keys():
total_margin += int(margin[i])
cnt+=1
print(total_margin/cnt)
모범 답안
with open('data/chicken.txt', 'r') as f:
total_revenue = 0
total_days = 0
for line in f:
data = line.strip().split(": ")
revenue = int(data[1]) # 그날의 매출
total_revenue += revenue
total_days += 1
print(total_revenue / total_days)
'자동제어 > Python for robotics' 카테고리의 다른 글
로또 시뮬레이션 (0) | 2023.02.19 |
---|---|
단어장 만들기(with open(), input(), break, .write()), 단어 퀴즈, 랜덤퀴즈 (0) | 2023.02.19 |
숫자맞히기 게임 (0) | 2023.02.19 |
datetime 모듈 (1) | 2023.02.19 |
random 모듈 (0) | 2023.02.19 |