Unable to create nested loop

I am unable to create a nested loop in python. My difficulty comes from having to add to the loop at the end.

Here’s the problem:

Within the sales_data loop, go through each location list and add the element to your scoops_sold variable. By the end, you should have the sum of every number in the sales_data nested list.

Here is my code:

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

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

The last line is what is messing me up. I’m also having a tough time understanding the concept of the variable that comes after the ‘for’ statement.

1 Like

I go into an ice cream store, they have 5 flavors of ice cream (vanilla, chocolate, strawberry, pecan, raspberry).

They give free samples. So I tell the worker for every flavor of ice cream you have, can I please have a free sample?

And so the worker will go through all the flavors individually and give me a sample. At every point, I am only taking in 1 flavor at a time.

The code for this looks like

ice_cream_menu = ['vanilla', 'chocolate', 'strawberry', 'pecan', 'raspberry']

for flavor in ice_cream_menu:
   sample(flavor)
   #sample is a made-up function for the sake of this narrative
   #the specific order I will sample the flavors is the order from the list

Now, it’s crucial that one is comfortable with playing around with different variants of the first example… we could just as well write:

ice_cream_menu = ['vanilla', 'chocolate', 'strawberry', 'pecan', 'raspberry']

for chicken in ice_cream_menu:
   sample(chicken)
   #this is the same code

but it is hard to understand (and a disgusting visual mix), so we try to choose semantically accurate variable names (and if we can’t efficiently come up with it, single-letter variable names, i, j, etc…)


Finally, once you practice enough to get used to that, the narrative changes. My friend tells me, that my favorite ice-cream shop is not the best in town. I heavily disagree. There’s only one way to find out. We go to all 3 shops in town. Conveniently they all have the same menu. We try all 5 different flavors to be scientific.

The code would look like this:

ice_cream_shops = ["ben_jerrys", "haagen_daaz", "ciaobella"]
ice_cream_menu = ['vanilla', 'chocolate', 'strawberry', 'pecan', 'raspberry']

for shop in ice_cream_shops:
   for flavor in ice_cream_menu:
      sample(flavor)

# we will go to each shop in the order listed, 
# and in each shop sample the flavors in the order listed.

It’s a silly example, and not actually fully accurate in our narrative since we would have to use classes to assign each menu to each shop (to properly give a “rating” to each one). But what it does show is that we did eat all 5 flavors in all 3 locations. I hope it gives you an idea of how loops can be visualized.

3 Likes

@toastedpitabread

This is a much better explanation and helps me visualize the code. Let me take a look at my problem again. The only thing in your example, is that you have a function at the end. What is telling the loop to actually loop back around so you can try different ice cream shop/flavors?

Even though my code is inefficient and by a ton of trial and error I solved it, however I do not see where the “loop” happens in my code.

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

scoops_sold = 0
for location_total in sales_data:
  for location_specific in location_total:
      sum1 = sum(sales_data[0])
      sum2 = sum(sales_data[1])
      sum3 = sum(sales_data[2])
      sum_total = sum1 + sum2 + sum3
      scoops_sold = sum_total

The only thing in your example, is that you have a function at the end. What is telling the loop to actually loop back around so you can try different ice cream shop/flavors?

Right, the function here is the action of sampling the flavor (yum).

for shop in ice_cream_shops:
   for flavor in ice_cream_menu:
      sample(flavor)

Let’s say i put it here

for shop in ice_cream_shops:
   sample(flavor) #this won't work at all
   for flavor in ice_cream_menu:
      pass

This wouldn’t work (will throw an error) because although I am in visiting each shop, I haven’t yet said that I want to talk about all the flavors. If I just wanted one ice cream in each shop I might say

for shop in ice_cream_shops:
   purchase("strawberry")

So the original code reads as: for every shop, for every ice cream flavor, sample that flavor.

2 Likes

I see this as being two lists

  1. Locations
  2. Sales

The big brackets are the locations, [a, b, c]
The lists inside are the sales for each location, a_sales, b_sales, c_sales.

So I semantically I might write:

for location in locations:
   for sales in location:
     do_something()

Which would read as, for every location in all the locations, for every sale in that specific location, do_something().

It’s easy to get distracted by the variable name of sales_data. I think it’s incomplete in describing what it contains.

@jrb0831 specifically, it should be sales_data_by_location. It really holds locations first, sales_data second.

Though I could be terribly misinterpreting the context. I still think this is what makes it confusing.

2 Likes

This is brilliant. So it will reiterate through everything unless you tell it not to. ‘For’ and ‘in’ will go through everything within the ‘in’ by using what was set in the ‘for’ unless it is told not to in the next line.

I think I’m making some progress here.

1 Like

Agreed, the hardest part was visualizing this with the generic variable names.

I would re-draw the array as this:

macys_sales =[12, 17, 22]
bergdorf_sales = [2, 10, 3]
dior_sales =[5, 12, 13]
sales_by_location = [dior_sales, bergdorf_sales, macys_sales]
1 Like

Sometimes I have to revise my code if I don’t name my variables or functions well because it gets really messy if it’s too vague, and really tedious if it’s too long. Good organization leaves more energy for more coding.

3 Likes