Instructor: Hi guys and welcome back!
In this video we're going to talk about Loops in Python.
There are two types of Loop.
One that allows us to repeat some
code a certain number of times.
And one that allows us to repeat
code for as long as a condition is true.
Let's start with that one in this video.
So, we've got our code from the last video here
where we ask the user where they want to play.
And then, if the user's input was y,
we ask them to get our number.
And we told them whether the number was correct.
The number being defined up here.
Now, what we're going to do is,
we're going to repeat the game
over and over until the user asks us to stop.
So, what I'm going to do is, I'm going to change
this prompt a little bit before we begin.
And I will say, "Would you like to play?"
And then, I would give them the
options of y in capital, or n in lowercase.
This is a common convention in terminal based programmes
where you use one of the options
as uppercase and one of them as lowercase.
And that means that if they type anything
other than n, y would be the default.
So they have to specifically type n if they want to stop.
This is a common convention,
so you can use it in your own programmes as well.
What that means is that now we want to repeat
this code here, allowing the user to guess a number,
and then telling them whether it's wrong or not,
for as long as the user didn't type n.
So, if the user types anything except n,
we wanna run this code.
So, we no longer need this if statement there.
But what we want to do is,
we want to use a while loop.
So we will say, while the user input is not n,
so while user input is not n,
then we will repeat this code over and over.
What happens here is that we've got
the while keyword, then we've got a space,
and then we have our condition.
More or less likely if statement we have earlier on
but now this code here, makes the indented code
run over and over.
And what happens is, it starts up at the top
and it goes to the bottom,
and then when it reaches the end
of this block that's indented,
it goes back to the top and evaluates this Boolean again.
So, if this Boolean is true,
then it will run this again,
and then it will go back to the top,
and evaluate the Boolean again, and so on.
Of course, the first time that Python reaches line four,
this Boolean is also evaluated.
So, potentially the user could enter n to begin with
and then this loop would not run at all.
So, that is how the y loop works.
Now, of course if the user enters y,
then y would not be equal to n,
that's true, so this would run.
And then we we'll go back to the top,
and this would still be y.
And it still wouldn't be equal to n,
so this would run again.
And so on, forever.
If you don't change the condition here, then you're gonna
end up in an infinite loop.
So, what we have to do to begin with is we have to ask
the user whether they wanna play again.
So, what we'll do is, will we copy
this line here just for now.
And we will put it inside the loop,
but not inside the else.
So, just down here in the same indentation level
as all the other lines,
we ask the user whether they want to play again.
And what that means is that when we go back
to reevaluate this condition,
the user can potentially affect that
and potentially terminate the loop.
However, there is a small issue in here,
which is that you have these two lines duplicated.
So, if you ever wanna change your prompt
for example you're gonna have to change it in both
places and that can be a little bit cumbersome.
So, instead what we're gonna do is,
we're going to get rid of it and introduce
another Python keyword that allows us to do that.
Here, I'm going to change this so it no longer
uses the condition.
Instead I'm going to do while true.
This always creates an infinite loop.
So, now this code will run and then we will
go back to the top.
This is still true, so it runs again.
And so on, forever.
If you use while true,
then you must be able to terminate the loop
from within the loop's code.
So what I'm going to do,
is I am going to move this line
into the loop.
And then after the line,
I'm going to say,
if user input is equal to n,
and here comes the break keyword.
The break keyword allows us to exit a loop.
So, because we're inside the loop here
we can break out of it and that terminates it and continues
our Python code from the next line after the loop.
So what we've done here is,
within the loop, we ask the user whether
they'd like to play, and if they type no, then we terminate.
And if they type anything else,
then we continue with the loop.
And that happens over and over.
This is an alternative way of doing the loop
that you saw just a moment ago.
You can do either this one does reduce duplication
but it adds an extra if statement inside your loop.
So, its up to you which one you wanna do.
Usually though, I would go for something like this.
That's everything I wanted to show
you regarding the y loop.
So, let's take a look at the For loop.
Let's say you've got a list of your friends
Rolf, Jen, Bob, and Anne.
And you want to print out a simple stream for each friend.
For example, you want to print out Rolf is my friend.
Jen is my friend.
Bob is my friend.
Anne is my friend.
The simple way to do that without loops,
is of course to do something like print,
friend zero is my friend.
And then repeat that for one, two, and three.
Obviously, as the list of friends grows,
these get increasingly difficult.
Plus, if you have this list as part of a programme,
and you wanna be able to print out that all
your friends, are your friends,
you're not gonna be able to do that without
changing the code.
So, here's where the loop comes in handy.
We're going to repeat this structure
for each of your friends.
So, what we'll do is we will say,
for friend in friends.
And then we will indent this piece of code.
And here we will use the variable of friend.
How the For loop works is,
you have the for keyword,
then you have your space,
then you define a variable
that will take the first value of this list.
So, Rolf.
And then you have in friends.
That's the list that you wanna go over.
And what this allows you to do, is to use this variable
and make it equal to the first value in this list,
and then run this code.
When you reach the end of the indented code, it goes
back to the top, and friend takes the second value.
And then it runs this code and it then it goes
right to the top, and friend takes
the third value and so on.
When you reach the end,
the For loop structure knows that you are done.
And then it continues running the Python
code from the next line onwards.
So, this is great when you wanna do something for each
element in a list, or a tuple, or a set and so on.
And also when you wanna repeat something
a certain number of times.
For example, here you know you have four
elements in this list.
So therefore this loop will run four times.
If you wanted to run a loop four times,
and you didn't care to create a list of four elements,
you can actually do for variable
in range four.
And that is also going to allow you to repeat
this code four times.
Of course the friend variable still is used.
So, if you run this code, you'll actually
see that you'd get back.
Zero is my friend.
One is my friend.
Two is my friend
Three is my friend.
Because when you do range four, that creates
something like a list
where the first element is zero,
the second one is one,
and so on.
This is a construct that allows you to iterate
over a list of numbers so that you can repeat the code
a certain number of times.
Usually though for a for loop,
you'll be using something like an existing list and do
something with each element.
Let's use a For loop to calculate the average
grade of this class.
So, let's say we've got a list of grades
where the numbers are
35, 67, 98, and two 100s.
And we have a variable total
which is equal to zero.
Then we also have an amount of grades,
which is the length of the grades list
which gives us five.
What we wanna do is use a For loop
to modify this total variable
and add to it each of these grades.
How we do that is we say,
for grade for example in grades, and that creates
a new variable that we're calling grade
although you can call it whatever you want,
and is going to help us go through each of the values
in the grades list.
Then we will say,
total plus equal grade.
And this by the way, I think we've not see it before
but it's the same as doing total equal total plus grade.
So, it adds up the existing total plus the new grade,
and sets it back to total.
You can do that or you can do plus equal is the same thing.
And then, after the For loop,
once we've added them all together,
we can print out the total divided by the amount.
And that's gonna give us the average.
So if we run this piece of code here,
you'll see that the average is 80.0.
Now, say at this point that you don't actually
need a For loop
for this specific piece of code.
And this'll help me introduce another function in Python
which is the Sum function.
Instead of doing all of this,
you can actually say that the total is the sum of grades.
Just as we have Len to calculate the length of a list,
Sum calculates the addition of all
the elements inside a list,
or a tuple, or a set, and so on.
So, here we have the total and the amount
and that's exactly the same thing
without the need for a For loop.
So, you just kind of have to know that this exists
if you wanna use it.
But, my advice would be to pretty
much search for everything in Google or your search
engine of choice.
I would search when I was starting,
for how to add a list of numbers in Python.
And that will give you many answers.
And one of them will teach you about this Sum function.
When you're doing anything else in Python,
usually I would also search for everything.
And that way, you're going to be constantly
absorbing new information and learning
different ways of doing things.
It's very often that programmers will try
a very complicated solution at first
because that's what we can build
based on the building blocks that we already know.
But, there's usually better solutions out there.
And by searching for them,
you're going to be able to absorb that knowledge as I said.
That's it for this video though.
Thank you for joining me.
And I'll see you in the next one.