Now I'm coding to get Change value (A day - A day of yesterday) and the data existing only working day so if no existing data of a day the code get KeyError. so I used Try, Except code but...so messy..I wanna combine below code.
ydate = datetime.strptime(date, '%Y-%m-%d').date() - timedelta(days=1)
sydate = str(ydate)
try:
yclose = data['Time Series (Daily)'][sydate]['4. close']
except KeyError:
try:
ydate = datetime.strptime(date, '%Y-%m-%d').date() - timedelta(days=2)
sydate = str(ydate)
yclose = data['Time Series (Daily)'][sydate]['4. close']
except KeyError:
try:
ydate = datetime.strptime(date, '%Y-%m-%d').date() - timedelta(days=3)
sydate = str(ydate)
yclose = data['Time Series (Daily)'][sydate]['4. close']
except KeyError:
try:
ydate = datetime.strptime(date, '%Y-%m-%d').date() - timedelta(days=4)
sydate = str(ydate)
yclose = data['Time Series (Daily)'][sydate]['4. close']
while Trueloop and return the values at the end of thetryblock. If thereturnis not hit, the loop will do an other round.