# Week 1 Assignment 2 Training the model part

How do I set index using for loop ? I also have to use modulus operator. I am so confused. Help !

1 Like

If you are working on the “model()” function, there is only one for-loop, and it was already set up for you.

``````    # Optimization loop
for j in range(num_iterations):
``````

What exactly are you working on? What index are you referring to ?

Hi Swetalin,

The number of iterations is larger than the number of examples in the list of dinosaur names. So when you go through the loop at some point you run out of examples in the list. Because of this you have to start again from the beginning of the list.

In order to do so, you can use the modulus operator. This operator takes the remainder after a division. An example may clarify:

8%4 = 0 (when you divide 8 by 4 the remainder is 0)
9%4 = 1 (when you divide 9 by 4 the remainder is 1)
10%4 = 2 (when you divide 10 by 4 the remainder is 2)
11%4 = 3
12%4 = 0
13%4 = 1

So when you go through the loop and reach the total number of examples in the list, how could you set the index to get back to the beginning of the list?

Good luck!

1 Like

Here at Set the index step, Is the given for loop in the code the same mentioned here?

Here j is given as the number of iteration. So from what you said
index = j % length of list
I did that and got the result.

The variable name is “idx”.
The last hint tells you to use the modulo operator.

Did that got the result. But I have another question. How do I change j or idx value or get another name from the list?

Hi Swetalin,

j is an iterator variable that runs from 0 to num_iterations-1. The value of idx changes with the iterator variable. As a result, idx can be used to select subsequent names in the list.

Hi TMosh,