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 :
Traceback (most recent call last):
File “”, line 18, in
x = get_returns(amazon_prices)
File “”, line 15, in get_returns
y = calculate_log_return(i, i+1)
File “/home/ccuser/workspace/analyzing-stock-data/”, 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:

Here is my code:

Write code here

def get_returns(prices):
returns =
for i in range(len(prices)):
y = calculate_log_return(i, i+1)

x = get_returns(amazon_prices)

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