I am doing exercise 10/13 on nested loops right now, I continued to fail and checked the solution but now I have the following question:
why does scoops_sold += elements need to be added in the nested loop:
for location in sales_data:
for scoops in location:
scoops_sold += element
It was not necessary during previous exercises and steps, which were also on nested loops.
Here is the link to the exercise, and I also included a screenshot. Would be amazing, if someone could explain it to me because I really want to understand the logic and what I need to do. Looking forward to some replies, thank you all very much in advance
I think you used the wrong name for the variable there:
scoops should be
element should be
It seems like those should be the same variable.
Here’s a similar example:
list_of_lists_of_ints = [
[2, 4, 3],
[1, 6, 5],
[7, 8, 1],
[6, 2, 0]
# want to get total of nested list, using nested loops
total_so_far = 0
for sublist in list_of_lists_of_ints: # sublist is the inner list(s), one at a time
for num in sublist: # access each number in inner list as "num"
total_so_far += num # add each number to the total
# total_so_far now contains the total for everything
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.