FAQ: Learn Python: Loops - Nested Loops

We cannot tell where the block indentation is but those last two lines should not be in the same loop as each other. Something to check, there.

I do not understand why I am getting this error as the Stack class which was imported in the first line has the _get_name attribute.

I know you have no reason to print out the sales_data underneath the outer loop, but why is it outputting 3 lines of the sales_data? I need to understand the narrative, is it because it is 3 sets? Please explain why it would print out 3 rows of sales_data and not just 1.

for location in sales_data:

[[12, 17, 22], [2, 10, 3], [5, 12, 13]]
[[12, 17, 22], [2, 10, 3], [5, 12, 13]]
[[12, 17, 22], [2, 10, 3], [5, 12, 13]]

why does below print out this output?

for location in sales_data:

[12, 17, 22]
[2, 10, 3]
[5, 12, 13]

The code does not work if there is a space after ‘print’ in ‘print (scoops_sold)’:
“Expected scoops_sold to be printed.” error is shown
Here is my code which does not work (it works after removing the space):

sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
for location in sales_data:
  print (location)
  for item in location:
print (scoops_sold)

I found this lesson to not be clear.

You show lists with 2 elements, but then show us a list of 3 elements.

It appears (?) that python does not need this:

for a in b:
for c in b:
for d in c:

etc. rather, the one “for” statement breaks apart all elements of a nested loop?
IE the one statement breaks apart nested lists of any size? Right?

IMO the lesson needs to make this more clear.

This is the first time I’ve seen code academy not do a good job explaining how to use a piece of code effectively.

Without actually going to the lesson, consider the following code…

>>> s = [
          [12, 17, 22], 
          [2, 10, 3], 
          [5, 12, 13]
>>> n = len(s[0])
>>> t = [0] * n
>>> for col in range(n):
	for row in range(len(s)):
		t[col] += s[row][col]

>>> t
[19, 39, 38]

When given a two dimension list, it will take one nested loop to access all the locations in every row. The above example is two dimensions with equal length nested lists. We want the sum of the columns, not the rows.

Note how we set the outer loop to point to the elements within the rows, and the inner loop to iterate the rows. A running sum is kept in a list which corresponds to the columns.