Calculating Financial Statistics : Help to iterate properly the lists of return

Hi all,

I am trying to figure out what’s wrong in my below code that gives back the following value while running :
1699.8
Traceback (most recent call last):
File “script.py”, line 18, in
x = get_returns(amazon_prices)
File “script.py”, line 15, in get_returns
y = calculate_log_return(i, i+1)
File “/home/ccuser/workspace/analyzing-stock-data/utils.py”, line 6, in calculate_log_return
return log(end_price / start_price)
ZeroDivisionError: division by zero

It gives the first price of “amazon_prices” and then specify a ZeroDivisionError meaning I’m trying to divide by 0, I guess the issue comes from the indexing but I’d need some help. would you mind to help me ?

Here is the link:
https://www.codecademy.com/paths/finance-python/tracks/introduction-to-python-for-finance/modules/calculating-financial-statistics/projects/analyzing-stock-data

Here is my code:

Write code here

def get_returns(prices):
returns =
for i in range(len(prices)):
print(prices[i])
y = calculate_log_return(i, i+1)
returns.append(y)

x = get_returns(amazon_prices)
print(x)

Hey @ruby4718216394, Welcome to the forums!

This seems to be happening because instead of using the prices list, you are using just the index. Try used “i” as a list index instead like so:

y = calculate_log_return(i, i+1)

But instead

y = calculate_log_return(list_name[i], list_name[i+1])
1 Like