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