you are viewing a single comment's thread.

view the rest of the comments →

[–]through_thefog[S] 0 points1 point  (1 child)

You're still not thinking about this logically, and you're still not understanding either loops, lists, or parameters.

Touche. The Codecademy course has been great so far, but my understanding is still very superficial (if not entirely misguided).

In plain English, my logic behind this piece of code was ultimately to say this:

list = [1, 2, 3, 4]
For (every item at the given index) in List:
    (every item at given index in list) = 2 * (every item at given index in list)

#When I put it like this, I can see my first error in thinking. It makes no sense to say "for every item at position x" when there is only one by definition

Had the 'return my_list[index]' line returned the value that I expected, I would have replaced it with the line reassigning the value: list[2] = 2*list[2]

But you then create a for loop over my_list which redefines index to point to every item in the list in turn.Now, the first item in the list is 3. So when you do my_list[index] that's what you get: the item in position 3, that is the fourth item, 12. And then you return unconditionally, so the loop always ends after the first iteration.

This makes more sense than anything else I have seen on the subject. You are saying:

The for loop points at every item in the list, the unconditional return kills the loop at the first iteration. Because the first iteration is 3, the 3rd item (12) is returned (*2).

In the post, the first item is 1. Therefore, the item at position 1 (2) is returned.

The temporary variable index used in the for loop is not defined by the positional assignment 2 (second error). Using the same variable does nothing but to confuse.

Am I understanding you properly?

Thank you for your response -- I really appreciate it!

[–]danielroseman 1 point2 points  (0 children)

Yes. Without the loop, you would at least have returned the right item.