Okey
dokey.
It is now time for us to try to put together everything that we've learned so far
to write a program.
And the program
we want is the one that will prompt the user
to keep entering a positive number.
Or else if they want to exit the program, they can simply type in the words or,
or the word done to exit the program.
OK? So
basically by saying, hey, user, keep adding a positive number,
keep on adding a number, keep on adding
N number.
Whenever you're ready to exit the program, just type in done right
now
as the user is typing in the numbers 2349 11, whatever it is,
we want to add those numbers to a list.
We want to be able to keep track of all the numbers that the user keeps on providing us.
So we're gonna add those numbers to a list,
but we also want the program
to check
the even numbers that the user has added
and keep track of the very first even number
that was provided
so that we can print out and tell the user that, hey,
this was the first even number that you added.
And then we can indicate what that number was.
Otherwise,
if the user added only odd numbers, then we can just simply say, OK,
no, even numbers were found in your list.
Now,
once the user has added several numbers and
then finally decides to exit the program,
we want the program to print out all the numbers that were added by the user.
And if the user never even added a single number,
just say no numbers were entered.
OK?
So let us tackle this step by step. First of all, we're going to want a list, right?
Let's call that list numbers. OK? So
we're gonna be adding all the numbers entered by the user.
We're going to add them to this list called numbers.
So obviously we're going to make use of the input function to, you know, prompt
a user
to add numbers, right?
OK.
And we're gonna have to create a while loop in this scenario because
we want the user to keep on adding a number or
providing a number until they choose to exit the program.
So
we can say, you know, while true and then the user keeps on adding a number.
And then we want to check
if the number
that was provided by the user
was actually
an even number. OK?
So we're going to also have some sort of like
an el statement that will check if the number,
if it's DVI
by two, you know that equals zero, you know, then print the even number,
you know, stuff like this, you know, otherwise just say
no even numbers are found.
But what else?
Remember that we also want to keep on adding the numbers that the user has provided.
We want to add them to the list called the number. So
we are going to have to append
the user input to numbers
list, right?
And we're gonna make use of a new function here called append.
All right. Don't worry, I'll show you how that works.
Uh What else do we need to keep track of?
That's pretty much it at this point, right? So
first of all, we create a list called numbers.
And then we're gonna have to prompt the user to add the numbers,
check to see if the numbers are even
once the very first even number has been found, let the program keep track of that.
And then at the end, print out what that even number was.
And then of course, also at the end of the program, most user have to say to exit it,
simply print out all the numbers that the user added.
So
let us do this. All right, I'm gonna move this away
and let us begin. So first things first,
let us create our empty list that will contain all the numbers of anybody user.
So numbers equals
and then T OK? So
that's the first thing I'm going to do. Now
let us prompt the user
to add
the number. So I'm gonna say while
and now true
colon.
Now we w we wanna prompt the user to provide some numbers.
However, we'll have to assign users input to a variable. So
let's say user underscore input, this will represent whatever the user adds.
The user input in here
equals to input
brackets.
And now let us prompt
the user
to enter a positive
number.
ok? Or
in brackets, they could also just type type
a da
to
finish
10.
So far. So good,
we've assigned
numbers. We have the empty list right there.
This will collect the numbers provided by the user.
And now we've created our loop to prompt the user
to keep on adding a positive number or type done
if they decide to finish. So
what we're gonna do right now is let us check to see if the user
has actually
typed the letters done or typed, done
because because we don't want the program to keep
on running if the user decides to type down.
So
I am going to say
if
user
underscore
input
is equal to
done,
if these are actually types done,
what are we going to do?
You guess that we are going to break the loop?
All right, let me add
some space in here.
OK?
Alright.
So
we have checked to see
if this
has typed in done
if they did break the loop.
However,
if they typed an actual positive number. What do we want the program to do?
We want the program to add that number to the numbers list?
OK.
So how are we going to do this? We need something to represent
the numbers that will be provided by the user.
In this case right now, don't get it twisted the user input.
The variable in here is simply
representing
the input in here, enter a positive number or type down to finish.
That's what it's representing in here.
The actual numbers themselves, we have to create a new variable for that. So
for the variable in here,
I am going to say no,
OK. Num
is going to be equal
two
int remember the integer
function that will convert whatever the user types in. So
it's not going to be in integer
and now underscore input
OK? So basically whatever the user types at
this line right here where we say enter a positive number
if the user types in 5.2 for example, OK, convert a 5.2 to an integer,
which will now be five and then assign five to the variable called number num.
Now the next step will be to add this number that user has added to the numbers list.
And how do we do that?
I'm going to type in numbers
and now check this out dots are
handed
and now in brackets.
And um
so this right here, this
is going to be the function
that will now begin to add the numbers added by the user to the numbers list.
Let's go over it one more time. So first of all, we have our empty list called numbers.
Let's create the loop called while
the prompt user to keep on adding a positive number.
However, if they typed done, if the user input is equal to done, break the loop,
no need to continue.
But if the user actually added real numbers,
assign those numbers to a variable called num num and now start to append
that those numbers to the numbers list.
So far. So good.
What else do we want to do?
We want to check to see if any even number
is being added.
So
what are we going to do?
We are going to create a for
loop.
OK?
That loop
is going to go through all the numbers in the numbers list
and then find the very first even number. So check this out.
I'm going to say four
norm
in, in
what?
In numbers? That's right. Now, let's add a colon.
Let's create our if
else statement. So if numb
divided by two equals to zero,
what do we want the program to say? We want the program to say print
and now
our brackets,
let's add our F string.
And then we can say uh found
the first
uh the first even number
and then of course, uh curly braces
and then numb.
OK.
Otherwise,
if the number that was added by the user isn't even
simply break,
let's just break it.
OK?
And now we can add our L statement to do something and
we can say
colon and now
we're going to say
a print
and I say no even numbers
uh were entered
by the user.
And there it is. However,
again, the identation is very, very important. Keep in mind that this
is under the four, OK? So it's not, it's not,
it's not if else
is the four Ls
are statement in here. So
just make sure
that you don't make the mistake.
The identation are gonna be very, very, very, very, very important. So
it's basically four and then number in numbers, if nor
divided by two equals zero points,
we find the very first number and then print out what the number is.
Otherwise simply break the loop once that number has been found,
once the very first positive number has been found.
So the first even number has been found
and then if no even number was added by the user at all,
they just simply print out no even numbers were entered by the user.
OK?
Last, but not least we now have to print out all the numbers
the user has added at the end of the program. So
I'm gonna come out here right now,
add the new if statement
and simply say if number. So if the user actually added some numbers, so if numbers,
guess what? Simply print.
And now I'm gonna say F
and now in codes,
the
numbers
you entered
and then
curly braces.
Now,
numbers
otherwise.
So that's going to be
simply prints. That is if the user did not add any numbers,
but you just say
no numbers were entered by the user.
Let us try.
I'm gonna go ahead right now.
We're on the program
and 0000, so we do have a bit of an issue right here. It says line 23.
OK. Print f what did I do wrong in here?
Oh, there shouldn't be any space. Sorry,
there shouldn't be any space right there.
You see how just a single space can ruin everything.
It's, it's kind of infuriating but yeah,
so whenever using the F strings,
make sure there's no space between the F and your code.
So let's try that again, run.
OK? So
I'm going to add one.
All right. Let's add three.
Let's add four.
OK. Let's add five.
Let's add eight,
let's add six.
And now I'm going to type in done
enter
and there you go.
The first number first, even number was four.
And now we've typed in the other numbers which are 13458 and six.
So you can see right now
the program actually works.
So let me run through the program one more time. First of all,
we created
a list called numbers.
This will hold
every single positive number added by the user.
Next, we created the while
loop
that will continually prompt the user to keep on adding a positive number or
if they choose not to,
they can simply type in the words or the word done to finish the program.
So we now need to check if the user actually typed in, done.
And by the way, let me even try that.
OK, let's run and let's just type in done instantly and see.
Well, there you go.
So the program works very, very well.
So it actually checked to see, hey, did this user type in done? Oh, it did. OK.
So if we typed in done,
then simply
break the loop,
we don't need to run the loop any longer because the user has typed in done.
So
in that case, right now, in this scenario where the user has typed in done,
basically all of this,
all of this stuff right here
is completely irrelevant.
OK?
The Python program will go straight from this,
from the break statement all the way down here
to simply saying, hey,
uh first of all, all, all the way right here.
So first of all say, hey, no, even numbers we were entered by the user
and then finally,
no numbers were added by the user. So all this
normal calls integer, if not uh is gonna skip all of that.
But what if the user actually typed in
a number?
What do we want the program to do. Well,
we want to add that number to our numbers list.
So we need to assign the user input to a variable.
In this case right now, we created a variable called norm
that will represent the input provided by the user.
And we also added the int in fact, let's try this and see.
I'm gonna say 4.5
enter
and 0000 OK.
There is a bit of an issue right here. We do have an error message.
Don't worry,
I'm going to show you how we can handle errors in the next video. OK?
But let me run this one once again
and type in, let's say five,
OK?
Type in five type in eight,
done.
OK. So it still works fine.
So
we made sure that we've assigned the numbers added by the user
to the variable called num.
And then we appended num to the numbers
list using this particular function right here.
Numbers dot append
in num.
OK.
Now,
because the user actually added some numbers,
we wanted the program to check if any of those numbers were actually even.
And, and by doing so,
we had to create a loop that will run through all the numbers in the numbers list.
And then if the number was divisible by two with no remainder,
simply say, OK, we found the very first even number printed that number.
Otherwise, if the user did not add any even numbers simply say no,
even numbers were entered by the user.
And at the end of the day,
simply print out the numbers that were added by the user.
And if the user did not add any number, simply say no numbers were entered by the user,
that's exactly what we did
right here.
So feel free to go through this program as many times as you need.
Hopefully, it was relatively straightforward.
And of course, as always,
if you have any questions about anything we've done in here that you're not,
you know, you're not exactly sure.
Always feel free to reach out to me.
I'll be more than happy to answer whatever questions that you have.
So German
next video where
we'll now begin to take a look at how we can handle errors in our program.