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 ?
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?
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.
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