Instructor: All right, it's exercise time.
Let's combine everything that we've learned
into this challenge.
What I want you to do is to create a function
called highest even.
And this function is going to take a list, data type
and this function for now will just pass.
But I want you to fill in this part
with whatever you think is going to
allow us to achieve this.
If I do print, highest even
and I give it a list of let's say, 1, 2, 3, 4, 8.
I want the highest even to be printed in this list.
And actually, let's do 10 here.
So the highest even number in this list is 10.
Just for fun, let's add 11 as well.
So the answer should be printed here as 10.
So based on everything you've learned so far,
try to fill this part so that you
create your own function, highest even.
Pause the video here, give it a try.
Otherwise, I'm gonna go with the answer.
So what I wanna do here, let's think about this,
is I wanna make sure that, first of all,
we only consider the numbers that are even in here.
So maybe what I'll do is create a new list called Evens.
And Evens is going to be an empty list for now
but I'm going to populate this with all of the even numbers.
So I'm going to remove three and 11 from this.
So let's do that.
I'm going to say for item in L I.
If item
modulo two
equals zero, which means it's going to be even
then I'm going to even
append the item.
So I'm gonna add it to this new list.
And then finally in here, once we have the evens list
I wanna find the highest number.
Now you can do this manually and go through it one by one
or Python comes with a nice function called Max.
And as you can see, we give it an iterable
and it'll tell us what the max number is.
In our case, we can just give it evens.
And remember, we wanna return something from this function.
That is what is the highest number?
Well, we wanna return whatever the max of the evens is.
If I run this,
I get 10.
Awesome, looks like it's working.
Now some of you may have gotten tricked and tried to
return inside of the for loop, but if we do this, remember
that this still works, but we actually return
from the function when we hit our first item.
If we had another item,
let's say two in here, and I click run,
we get two.
And why is that?
Because I'm returning.
When we loop the first time through our item
which will be two.
Is two an even number?
Yes it is.
So we're going to append and then we're finally
returning max evens, which only contains the item two.
So you wanna make sure here
that the indentation really matters.
And if you're returning something from inside a loop
you wanna make sure that that's your intended behavior.
You usually wanna loop through something and then return.
So make sure you don't prematurely exit the loop.
Nice and simple.
Remember, your answer may be different than mine.
There are many ways to go about this, but hopefully
by practicing this, you have a better idea of functions.
We now have a great utility function
highest even that we can use all over our program.
Good job getting this far, and I'll see you in the next one.
Bye-bye.