Analyzing Stock Data

i am getting stuck of this exercise, there is no help or view code in this solution, I was stuck at part 4.

"As you iterate over each index i , the element in the prices list that is at position i will be the start price and the element with index i + 1 will be the end price. Use the calculate_log_return() function imported from utils.py to calculate the rate of return from the start and end prices. Then, append the rate of return to the returns list.

After your for loop, return returns from the get_returns() function."

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

Anyone can help? Much appreciated!

Hi,

I’m about to log-off but since it’s been a while and you haven’t been replied to I’ll copy what I wrote in another post. The topic you may want to review is for-loops and lists (at least). But let me know if your question/problem is more specific than that and I can chip in!

  • a for-loop is a very common and powerful tool in programming. Don’t let its ubiquity fool you, it is very handy and often has surprising ways of being useful.
  • a for-loop can be translated into plain English with a sentence like: for each item in your list , do this action .

In code that would look like this:

for item in list:
    action

Note, that the only 2 hard requisites are that the list be an iterable object (which basically means you can cycle through it. I think of it like numbered key lockers), and that the actions have to be valid.

What is flexible is the naming of the item — it’s just a variable place holder. Often you will see the letter i pop up as a popular variable:

for i in list:
   action

So now for a real example:

list_1 = ["pita", "bread"]
list_2 = ["is", "a", "viable", "food"]

for item in list_2:
   list_1.append(item)

print(list_1)
#output
#["pita", "bread", "is", "a", "viable", "food"]

In plain English, this code’s commands are to:

For every item in the second list, add that item to the first list. (By default it will go in order, later there are ways to make it go in reverse and all sorts of zany stuff, but basics are important first).

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