Https://www.codecademy.com/paths/finance-python/tracks/introduction-to-python-for-finance/modules/calculating-financial-statistics/projects/analyzing-stock-data

Can somebody explain to me what is wrong with this code?

def get_returns(prices):

returns =

for i in range(len(prices)):

log_return = calculate_log_return(prices[i], prices[i + 1])

returns.append(log_return)

return returns

amazon_returns = get_returns(amazon_prices)

ebay_returns = get_returns(ebay_prices)

HI @franko7554, please have a look at the following link on formatting code which makes it much easier to understand (proper formatting and indentation)- FAQ

I assume your code threw an error, it’s worht mentioning this too as it’s probably important. At a quick glance I’m guessing an Index Error ? What section of your code might be attempting to look up an index that doesn’t exist?

1 Like

Thanks for the tip, here is the proper version

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

1 Like

I am struggling to create a good range for a list, by using a for loop which iterates from 1 to 2nd to the last element of the list

I’d double check the instructions for this one carefully as it provides information on what range to iterate over. As it stands what happens on the last iteration of the loop?

Just found a way to omit this error, as the i + 1 was iterating out of range on the last attempt.
Thanks for all the help

Hello this is what i did and i do am aware that if i put i+1 it will give index out of range error, the problem is how do you prevent the “i+1” to get out of range?

Consider the calculations you actually need to perform. How many index values do you actually want range to produce?

1 Like

well according to the months i believe i want 12 indexes

What about the calculation you are using? What inputs/outputs would you expect from a sequence? Work it out with pen/paper if necessary and consider what would happen with fewer months (2 months, 1 month, 3 months for example).

1 Like

This is what I did, and i didn’t get any error from this, i put 12 on purpose because it will 12 months in a year always.

def get_returns(prices):
  returns=[]
  for i in range(12):
    returns.append(calculate_log_return(prices[i],prices[i+1]))
  return returns
1 Like