This is the solution for the exercises I gave you
in the previous lesson, okay to practice with
loops and conditions. So let's start with the
first one. I'm going to right click here in the
project New, Python file,
let's name it max_value, okay, max_value.py,
and I'm going to create a function to
compute the max value inside the list. So def,
compute_max_value_from_list. Okay, the name is a
little bit long here, but it's quite explicit. And
then I have number_list as a parameter, okay. So
I have the name of the function, the parameters.
Now I also know I will need to return something,
okay. So at the end, I will have return
something, I'm going to create first variable
named max_value, okay, which I'm going to
initialize to zero, okay, and return that at the
end. Now, what I'm going to do is I'm going to go
through all of the elements of the list, and I'm
going to check that against the max_value. Okay,
if one element is greater than max_value, I'm
simply going to update the max_value, okay with
the new element. So at the end, I will have the
max_value inside this variable, and I can return
it. Okay, so let's use a for loop. for number in
number_list. For each number, I'm going to check
if number is greater strictly greater than the max_value,
in this case, I'm going to do max_value is
equal to number. And well, that's pretty much it.
So for each element, if the element is greater than
any value we had before, we update, so we set the
number, the actual value that is in the number
variable right now, to the max_value variable, here,
so at the end, we'll return the max_value. And
now that we have the function, I'm going to create
a list. So let's say number_list, I can use the
same here, because this is a different scope.
Let's use integers. But you can do with float
numbers if you want, 3, 2, -3, 9,
and then 4. And then max_value is equal to
compute_max_value_from_list, I use the auto
completion and I pass the number_list. And then
again, print max_value. Okay, let's run that. So
actually, this is loops, I'm going to run max_value here.
And you can see max_value is nine,
okay, so you can make a nice print if you want to
here, okay, and we have correctly solved that
problem. Here, we have the max_value here, nine,
okay. So if we go through the function here, the
first number is going to be 3. So if
3 is greater than zero, yes, we update, and
then 2, 2 is not greater than 3, because now
the max_value is 3, so we don't enter that if.
The next time -3 doesn't work either for
the if, and then we have 9. 9 is greater than
3, which is the current max_value. So we
update max_value to 9. And then 4, we
don't update because 4 is lower than 9, and
then we return 9. Okay, now let's go to the
second exercise, I'm creating a new Python file
named get_numbers_from_user, I'm going to put it
_1, okay, so we have 2 after that. So we first
need to ask the user to give five elements, okay,
add them in a list, and then compute the average of
that list. So I'm going to create a number_list
here, which is empty. And then I'm going to do for
i in range(5), so this is going to be executed
five time number_list.append, I'm going to
append a new number and this is going to be input,
Give a number: and also I am going to cast this as an integer.
Okay, you could use float also. Actually, let's
use float that maybe better, so we have a bit of
precision and we use float number in the number_list.
So number_list.append(), and we add a new
element which is float so we cast the result to
float, from the inputs of what the user gives us,
okay, so now we have the list. I'm going to go back
to when new in addition. So zero in addition, and
then again, compute the average from that list.
And to compute the average, I'm going to go back
to the compute_average here, this function that
I'm going to copy, it's working. So I'm going to
copy this inside this program, okay, so compute_list_average.
And now what I can do, so I had
prints compute_list_average, I'm going to do
that also. So the list has the same name. Okay,
number_list. So once I have all the numbers I
need, I simply call compute_list_average to
compute the average and return the average and
then print it on the screen. Okay, let's run that.
So Run, get_numbers_from_user_1. So Give a number:
one, Give a number: 4.5, okay, let's say 3,
8, and 4. And you can see the average of
the numbers here is 4.1. Okay, so it asked me to
give five numbers and then compute the average
and print the average. Alright. And now let's do
the third exercise. I'm going to create a new
Python file, get_numbers_from_user, let's name it
_2. So when we needed to get exactly five numbers,
the for loop was the best option. Now we need,
what we need is, we need to ask the user to give
some numbers, and we are only going to stop when
the user gives us zero. Okay. So in this case,
you're going to see that we are going to use a
while loop. So first, I'm going to create an empty
list. Okay, initialize it to zero elements. And
then well, I'm going first to create a Boolean
variable. ask_user_for_number is equal to true, okay.
And I'm going to do while ask_user_for_number, and
while this is true, we're going to keep asking the
user for numbers, okay. And when the user gives us
zero, we are going to put that flag to false. And
then we are not going to enter the while anyway.
Okay, so this is quite common practice to
create a Boolean flag or Boolean variable. And
then in the while, you check some conditions, and
if some condition is met, you set that to false so
the next time, you don't enter while. So I'm
going to do user_input, I'm first going to get the
user_input, okay, I'm going to cast it as a float
input, Choose a number:. All right, and then I'm
going to check if user_input is equal to 0.0,
because this is a float. What we do is we set the
ask_user_for_numbers flag to false, okay. And then else,
I'm coming back to this indentation. So here you can
see the if is inside the while. So we have four
indentation, and then for this, we have four more
indentation. So we have eight spaces total. And
then I write the else, I need to write else at the
same indentation as the if, okay, but still with
four spaces because we are in the while. Okay,
else, I'm going to do a number_list.append
with the user_input. Okay, so what is it
going to do? First, this is going to be true
because this is tricky. We ask the user for a
number that we cast as a float, and then we
check if this is equal to zero. So if this is not
zero, we enter the else, we add the number to the
list, we come back to while, and we continue because
this is true. Okay. Now when the user gives the
number zero, this is going to be true. So what we
do is we set this Boolean variable to false, we
don't enter the else okay, because this is true.
We come back to while, this is false. And so
basically, we don't enter this block of code, then
we continue the execution of the code here with
zero indentation. And now what I can do is I can
compute the average so I'm going simply to get
this function again. Put it there.
And print compute_list_average. Let's use what
we have written before and print like this. Okay,
let's run that. So actually Run, get_numbers_from_user_2,
Choose a number:, I'm going to give two. Choose
a number: 5, 2, let's say -1, you can
see I can continue to give any number I want,
okay? And then I'm going to give zero. And as you
can see, once I give zero, this stops and this
prints the average that was computed from all
the elements here I have given previously. And as
an improvement here, maybe that you could also
create a function to do that, okay, and function
to do that, okay. I'll let you think of how you can
name the function if you need to give parameters
or not, etc. That's an additional practice. And
we'll alright, you have just finished delivering
three of these Python codes.