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:
<>print(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:
<>print(location)

[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]]
scoops_sold=0
for location in sales_data:
  print (location)
  for item in location:
     scoops_sold+=item
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.

For this problem I’m confused about the use of the terms “element” and “location”. Are these terms more built in functions of Python that were not explained before starting this lesson, or are they universal and could be changed to any other word someone would want to use?

if you look at the WHILE loop below, what has me confused is when it says for “location” in sales data and “element” in location. I’m not understanding how it’s tying into the 3 different groups of sales data in the brackets?

’ sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]

scoops_sold = 0

for location in sales_data:
print(location)
for element in location:
scoops_sold += element

print(scoops_sold);

Element is the generic term often used for the objects referenced by a sequence. [0, 1, 2] is a list with three elements, the first of which is 0 etc. etc. which extends to other sequences too. It’s not a keyword or built-in. There’s also nothing special about location in this case, it’s just a variable name. Considering the background of this questions perhaps scoops_sold or similar would be an acceptable name?

If you read the details in the instructions it states that each sublist in sales_data is the sales of three different ice cream flavours at each location of an ice cream franchise. Naming variables like this is common as it makes it easy to understand so location could indeed be changed (so long as it’s not a reserved keyword in Python e.g. do not overwrite list).

As for the loop itself have you seen the output of that print statement? For simple scripts they can be very helpful for debugging. Consider adding some more in the sections you don’t understand so you know for certain what values any variable has at any point you’re unsure about.

The first print would be [12, 17, 22] the first element of sales_data which is now assigned to location. In the next step a for loop iterates through location so your first element would be 12, the second 17 and the third 22. Maybe try adding a print here so you can see exactly how these loops deal with the nested list.

1 Like

for location in sales_data :

for scoops_sold in location:

scoops_sold += scoops_sold

print(scoops_sold)

why this is not givng the right answer ???

Any reason why the same variable is added to itself?

Why does

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

scoops_sold = 0

for location in sales_data:
  print(location)
for i in location:
    scoops_sold += i 

print(scoops_sold)

print 30 while

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

scoops_sold = 0

for location in sales_data:
  print(location)
  for i in location:
    scoops_sold += i 

print(scoops_sold)

prints 96?

What are the differences in those two sections of code? Remember how Python organises blocks of code? Try following in your head or with pen and paper what happens to the value of i at each step of your loop.

If you need to check then using print or similar can help you with that.

This is my code for the scoops_sold task:

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

scoops_sold = 0

for location in sales_data:
print(location)
for element in location:
scoops_sold += element

print(scoops_sold)

#based on the solution guide, this code should work; however, I’m getting 30 as a result rather than 96. Can anyone help me out as to why this is the case? Thanks!

Please check the following FAQ which covers amongst other things how to format code on the forums (Python without indentation is hard to understand).

Make sure you know exactly what you’re iterating through in each loop, you print out location, have you tried also printing out element?

Yeah, I trying fixing the indentation on the original post. Sorry about that. I haven’t tried printing element. I’ll give that a shot!

1 Like