This is the solution for the activity number three.
And in this activity you had to compute the max value from a number list.
So as a first step you just write the code here and as a second step with a function.
So let's do that.
Let's start with the first step.
We have our list.
I have already created a variable here max value that I initialize to what I need to initialize it with
something.
So.
So the first element of the list.
Now to be able to find the max value from this list, I will need to basically go through each element.
To go through each element I will use a for loop.
So for number because it's the number list, we use number.
In number list I put a colon and I go to a new line.
Now what do I do with each element.
Well I will simply compare that element with the current value that we have inside the max value variable.
So I can put an if here, and if I find that the number is greater strictly than the max value.
Then it means that I have found a new maximum.
So then I can say that the max value is equal to the number.
And that's how we find our max value.
And that's all the code that you need.
Okay.
So let's go back to this with a bit more explanation.
If you didn't manage to get this.
So we go through each element.
And so for example for the first element.
So we're going to go to the first element we're going to compare.
If so number variable is going to be equal to that element.
Max value is one at this point okay.
So is one strictly greater than one.
No.
So we don't enter this.
If so for the first element of course that's obvious because it's already this value.
But then we go to the second element which is five.
So is here five strictly greater than one which is the current max value.
Yes.
So in this case we enter the if and we say that the max value is now five.
After this we go back actually to the four because we still have elements in the list.
We go to minus six.
So is minus six strictly greater than five.
Because now max value is five.
Well no.
So we don't enter the if then we go with 12.
Is 12 greater strictly than five?
Yes.
So now the max value is 12.
And finally we do it with seven.
Is seven strictly greater than 12.
No.
So we don't enter the if and after that we exit from the four.
So I'm going to go back to indentation and print the max value.
And let's run that.
And the max value is 12.
Okay.
That's that one.
Okay.
And one thing that you can notice here that I talked about, but here we really do it is that well you
can combine any for and if so, basically any loop and condition, you can combine them inside other
ones.
Okay.
So here we have an if inside the for.
And then you see we have a block of code inside the if you can add another if and for etc..
Okay.
Just make sure that you have the correct indentation.
Because if for example that one is here, then that line is not in the if it's going to be directly
executed in the for.
So you want that to be inside the if.
Okay.
So for each new block of code inside the loop or condition you add a new indentation.
So here we have four spaces plus four spaces.
Great.
And now what we can do is create a function.
Because what we did here is we got the max value from a number list.
But what if you want to do that again with another list.
Okay.
In your program.
So what if you want to do this several times we can create a function to do that.
So I'm gonna I'm gonna write stuff at the beginning of the file here because you should first define
a function and then use it.
So let's do that.
At the beginning of the program I'm going to use the def keyword.
And then what do we want to do.
Well we want to get the max value from a number list.
So let's just write get max value from number list.
It's not the smallest name, but it's very explicit.
And I prefer to write explicit names than try to find very short combination of words or letters that
in the end are confusing.
So get max value from number list and we get a number list as a parameter.
That's going to be the list that we're going to work with.
So I put the colon and then I go back to a new line.
I have an indentation.
And what do I do in this function.
Well I do exactly what I did here.
I need to go through this number list.
So first I have to put a max value and then go through all the elements of the number list, find the
max value and return it.
So I'm just going to copy this code here.
So copy and paste inside the function.
So for example you can see it's like this.
But because it's in the function I need to add another indentation.
So here I select everything and I press tab.
And I'm just going to remove this space here.
You can add as many spaces as you want but I'm just going to remove the space.
So now you can see that for example this instruction here has four spaces here plus four plus four because
we have one two and three indentation.
So in this function we receive a parameter that's number list.
That's going to be local variable.
Inside this function we set an initial value.
And then we compute.
Well we use this for loop that I just described to get the max value.
But then that's not it.
We need to return this max value.
So I'm going back to a new line.
And I need to go back to indentations here to be On that level and I can do return.
Max value.
Okay.
It's very important to write the return here because if you do that the return is going to be inside
the for loop.
So after we check the first element we are going to already return from the function and not check the
other elements okay.
And if I do this, then as soon as we enter the if we are going to return from the function.
So you have the return exactly at this indentation and also not here because there it will be invalid
because this is the return outside of the function.
Okay.
So exactly here with this indentation.
Great.
And so now let's just remove that code here.
We don't need it anymore.
I have my list and I can do max value is equal to.
And then instead of doing all the computation again I just call the function get max value.
And actually, as I told you before you can use the auto completion.
So here with Tony you press Ctrl space and you have a suggestion, then you can press Tab or enter to
accept this.
And you already have the name and you're going to pass the number list.
Close the parentheses okay.
Let's just remove the space here.
Make it a bit cleaner.
So we create a number list.
We call the function.
We pass the number list and then we print the max value.
Let's run that.
And you can see same behavior.
We have 12.
And note again that here we have a number list.
And here as well.
But this number list is a local variable inside the function.
So that's why we can have it here.
It's not going to be the same.
And we also have a max value variable here and max value variable there okay.
But that one as you see is created inside the function.
So it's also local to this function okay.
So the max value variable is not the same as that one because different scopes.
Great.
And that's the end of this activity.