FAQ: Learn Python: Loops - Nested Loops


#1

This community-built FAQ covers the “Nested Loops” exercise from the lesson “Learn Python: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Nested Loops

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Im struggling to finish this code:

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

Im not sure how to make sure that each element gets added to the zero value variable. I dont know what Im doing wrong. Can someone show me and tell me what is going on here in this code?Preformatted text


#3

Try switching those two variables around and see what happens.


#4

wow what a simple fix. Thank you so much!


#5

I’m not sure why the code below doesn’t work:

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

scoops_sold = 0
for sales in sales_data:
for scoops in sales:
scoops_sold += 1

it saying that scoops_sold is 0.


#6

This loop iterates over the location list;

This loop iterates over the sales data;

The above should be adding the data to the result, not 1. We are not incrementing the list, but accumulating it to arrive at a final total. Looking at the numbers it should be somewhere around 96.

Be sure your indentation is correct.


#7

Thank you for the explanation


#8

hey, so i have this code in:
sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0

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

however when i try to run it it says:
TypeError: unsupported operand type(s) for +=: ‘int’ and ‘list’
idk how to change the the type or what to do


#9

iterate over the inner list…

for sale in location:

#10

How would I add only elements after a certain index value?
I have

sales_data = [['a',12, 17, 22], ['b',2, 10, 3], ['c',5, 12, 13]]

scoops_sold = 0

for location in sales_data[1:-1]:
  print(location)
  for element in location:
    scoops_sold += element
    
print(scoops_sold)

I want to add all values after the 0 index.
I tried

for location in sales_data[1:-1]:

but the loop skipped over the set list [‘a’,12, 17, 22] and went to [‘b’,2, 10, 3]
thanks in advance for your help!
Dennis


#11

Did you try,

for location in sales_data:
  print(location[0])
  for element in location[1:]:

#12

Many thanks! I did NOT try that variation.
Again, thank you!

sales_data = [['a',12, 17, 22], ['b',2, 10, 3], ['c',5, 12, 13]]
#add up only the second list 
scoops_sold = 0

for location in sales_data[1:-1]:
  print(location[0])
  for element in location[1:]:
    scoops_sold += element
    
print(scoops_sold)

b
15


#13

I would remove the bolded part. We want to iterate the entire sales_data.


#14

Duly noted! Thanks!
Here’s the new code:

sales_data = [['a',12, 17, 22], ['b',2, 10, 3], ['c',5, 12, 13]]

scoops_sold = 0

for location in sales_data:
  #print(location[0])
  for element in location[1:]:
    scoops_sold += element
    
print(scoops_sold)