본문 바로가기
Coding/초보자를 위한 파이썬 300제

초보자를 위한 파이썬 300제 // 180 list에 넣기

by 포스트it 2021. 2. 8.
728x90
반응형

 

문제

리스트에 5일간의 저가, 고가 정보가 저장돼 있다. 고가와 저가의 차를 변동폭이라고 정의할 때, low, high 두 개의 리스트를 사용해서 5일간의 변동폭을 volatility 리스트에 저장하라.

low_prices  = [100, 200, 400, 800, 1000]
high_prices = [150, 300, 430, 880, 1000]

내풀이

low_prices  = [100, 200, 400, 800, 1000]
high_prices = [150, 300, 430, 880, 1000]

for i in range(5):
    volatility = high_prices[i] - low_prices[i]
    print("변동폭 =", volatility)
    
결과값
변동폭 = 50
변동폭 = 100
변동폭 = 30
변동폭 = 80
변동폭 = 0

정답

volatility = []
for i in range(len(low_prices)) :
    volatility.append(high_prices[i] - low_prices[i])

리스트에 넣는건데 출력하는줄 알았다...
728x90
반응형

댓글