FAQ: Learn Python: Loops - Nested Loops

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

Thanks Man!! I was stuck and did know what to do :slight_smile:

Why do I get the error “The output does not look quite right. Is your final print() statment unindented?”?

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:
print(item)
scoops_sold += item
print(scoops_sold)

Welcome to the forums!

Try removing the line print(item). It might be that the exercise is just checking if the second print statement is unintended (it didn’t explicitly ask for you to print each scoop sold).

Thank you, it worked. But it’s a very illogical mistake.

If you want to be a programmer, get used to the fact that things don’t always play along to our liking. We fit the bill, or we don’t. It’s a world of, ‘keep trying.’

1 Like

I’m stuck on why and I don’t understand - one sums all of the figures, while one sums only those in the last list. But why that would be has not been explained or makes sense to me.

If you’re asking about the nesting it’s that Python groups blocks of code by indentation. So if I have a for loop every line I want to repeat should be indented. The next unindented line is not part of the loop at all, for example-

for _ in range(3): print("repeat") print("don't repeat\n\n")

If you want to repeat more than one line they must all be indented in order-

for _ in range(3): print("repeat") print("repeat this too")

Note that loops can be nested inside other loops. So in the given example we first look at a location, then we count how many scoops are made at this location using a loop but for a grand total this second loop must be repeated for each location. So we end up with a loop inside another loop as we want this second loop to repeat at each location.

If you want to track the details consider adding some print functions to your code so you can see at what point in the loop the code is currently executing. Maybe play around with the example below or your own code-

for i in range(1, 4): for letter in "abc": print(f"Outer loop iteration: {i}, letter = {letter}")

Sorry tgrtim, what I meant is that:

I don’t understand why the second part:

for a in location:
  print(a)

Only prints the elements in the last list of the three. I see that Python sees each list as its own element, and so a second loop is needed for Python to reach each element of each list. But I don’t understand why a separate print statement that is not a nested loop will only print out 5, 12, 13 and not all nine numbers.

The syntax of a for loop hides a couple of things. The first is that in each iteration you effectively assign a new reference to the target name, in this case location is being assigned to one of the nested lists from your sales_data such that location = a_nested_list.

As in your example when you print it out you can see a representation of the list object. But what that name refers to changes on each iteration of that loop. By the time you reach your second loop for a in location: ... location is only assigned to one list, the last list from sales_data.

You can try printing out location e.g. print(location) near your line 8 print("") if you wanted to see what it actually contains at that point.

1 Like

I have this same exact syntax but I keep getting an error through codeacademy

1 Like

Ref exercise 4 in the nested loops lesson.
I get the correct answer and my print statement is outside the nested loops so how come I get a “that does not look quite right…” comment and as a result I am unable to progress?

1 Like

I have just figured this out and the answer is dumb, I mean really stupid.
Did you like me delete the first print statement? If you did then reinstate it and you will get a nice blue cross!

1 Like

*** Edit***
I was able to fix the code because it required me to keep the print(location) from earlier in the exersize.

I keep getting the “The output does not look quite right. Is your final print() statment unindented?” but I feel like the code I have is not indented!

scoops_sold = 0
for location in sales_data:
  for scoops in location:
    scoops_sold += scoops
print(scoops_sold)

I have moved them around and put spaces but I can’t seem to get it to pass me. I am getting 96 though.

1 Like