 # 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 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)
``````

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

from utils import *

def display_as_percentage(val):
return ‘{:.1f}%’.format(val * 100)

amazon_prices = [1699.8, 1777.44, 2012.71, 2003.0, 1598.01, 1690.17, 1501.97, 1718.73, 1639.83, 1780.75, 1926.52, 1775.07, 1893.63]
ebay_prices = [35.98, 33.2, 34.35, 32.77, 28.81, 29.62, 27.86, 33.39, 37.01, 37.0, 38.6, 35.93, 39.5]

def get_returns(prices):
returns =
for i in range(len(prices)-1):
start_price = prices[i]
end_price = prices[i+1]
y = calculate_log_return(start_price, end_price)
returns.append(y)
return returns
amazon_returns = get_returns(amazon_prices)
ebay_returns = get_returns(ebay_prices)
print(amazon_returns)
print(ebay_returns)

I an having the same issue. When I print the function I should be getting a list of monthly returns but I only get one set of data. Any help would be welcome.

Hi all, im having this same issue of one value returned, i also got the zero division error as well.
Where is the soultion to this on codeacadamy i cant seem to find it?

1 Like

have you solved it ?

Hi @tera7463945260. Might need to see more of your code in order to diagnose the problem. If you’re submitting code to the forums please try and format it, there are details on this at the following-

An issue with division by zero was solved in the post above-

If it’s an issue with returning a single value I’d hazard a guess that your loop is terminating early, possibly with an issue with the placement/indentation of the `return` statement.

If it’s not either of those issues we’d need to see more of the code. Cheers.

hey thanks for the answer, here is my code :
this shows a number as a result instead of a list.
def get_returns(prices):
returns =
for i in list(range(len(prices)-1)):
start_price = prices[i]
end_price = prices[i + 1]
def calculate_log_return(start_price, end_price):
return log(end_price / start_price)
rate = calculate_log_return(prices[i], prices[i + 1])
returns.append(rate)
return returns
amazon_returns = get_returns(amazon_prices)
ebay_returns = get_returns(ebay_prices)
print((amazon_returns))
print((ebay_returns))
print(display_as_percentage(sum(amazon_returns)))
print(display_as_percentage(sum(ebay_returns)))

here is the result :
[0.06465576316168306]
[0.09472807078164892]
6.5%
9.5%

Please re-read the previous comment carefully, especially regarding formatting. If this is an indentation issue then the current format doesn’t help at all.

here is it , sorry for the previous one

``````def get_returns(prices):
returns = []
for i in list(range(len(prices)-1)):
start_price = prices[i]
end_price = prices[i + 1]
def calculate_log_return(start_price, end_price):
return log(end_price / start_price)
rate = calculate_log_return(prices[i], prices[i + 1])
returns.append(rate)
return returns
amazon_returns = get_returns(amazon_prices)
ebay_returns = get_returns(ebay_prices)
print((amazon_returns))
print((ebay_returns))
print(display_as_percentage(sum(amazon_returns)))
print(display_as_percentage(sum(ebay_returns)))
``````

Check the instructions for the lesson. You could avoid your rather curious use of a nested function definition. Skimming replies and instructions is a risky business.

As previosuly mentioned, an indentation issue could very easily cause your problems. What’s the purpose of `returns` as a list if you’re not using it.

the returns list is used to store the rates,the problem that I don’t understand is why is doesn’t produce a list instead of a number because we used the for loop, so obviously we should get a list isn’t it.
if you have any tips about I am ready to search for the answer because I have literally been in huge puzzle because of this project, I have searched in youtube and other forums but I could not found what am I doing in a wrong way

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.