Instructor: Welcome back.
Let's try a simple exercise.
I want you to build a simple counter.
And this counter is going to count the items in our list.
What does that mean?
Well, let's say I have a list,
and this is a reserved word.
Remember, it's an actual word in Python.
So let's name it my_list as a variable.
and this list has 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
What I want you to do is using looping
to loop over this iterable list
and sum up the total of the list.
Pause the video if you need to.
Otherwise, let's see how we can do that.
What I'll do is create a counter variable
that will equal to 0 to start off with,
and then I'll say for item in my_list,
I'm going to
counter = counter + item,
and then at the end,
I'm going to print counter.
Let's see if this works.
I'm going to hit run
and I get 55,
which, if my quick math is right,
should be correct.
Now, when doing this exercise,
you may have gotten tripped up a bit.
So let's think about this.
Some of you may have made an error
where you did the print here.
But if you did the print here,
you remember that it's going to get looped over.
It's part of this code block,
because of the indentation,
and it's going to print as many times as there are items.
So you have to make sure
that the indentation is like this
so that you get the total.
The other thing that you noticed
is that in order for you to keep a counter
or a total of all these items,
you had to make sure that you had a variable
outside of the loop,
because the loop runs the code over and over,
and you needed something on the outside
that doesn't change.
Because if you moved the counter to 0 here,
well, every time the counter will be reset to 0
so that by the time we get to 10,
counter will be 0,
so you get 0 plus 10, which equals 10.
And then you'd get, well, 10.
So you have to be careful here
that the indentation is an indication of us looping.
And when you loop something
and you wanna keep a tally or total of something,
that you have these issues
of making sure that what you wanna loop over
is inside of this code block.
But what you don't want to,
if you wanna have some sort of information
that's outside of this loop,
then you keep it like this.
If you weren't tricked by this question,
good job.
If you were, don't worry.
It's all part of learning.
I'll see you in the next one.
Bye-bye.