Jose: Hi Guys, and welcome back.
In this video, we're going to be creating the user menu
for our project so that we can start interacting
with users and looking at what our programme is going to do.
To create the project, I've gone ahead and made a folder.
I've called it Prog diary.
You can call it whatever you want.
And I've opened that with Visual Studio code,
in this case, you can use your editor of choice of course,
in here, I'm going to create a file called app.py.
This is normally the app I use to start a project
it's gonna contain the main logic of our application.
Now that I've got this file here,
we're going to start off by creating the main variable
that is going to be the menu that
we're going to show our users.
So I've gone ahead and written that out,
I've used the multi line string and we've got
the Please select one of the following options,
the three options,
and then their selection with a space at the end
so that users can go ahead and type after this.
We're going to be passing this variable
to the input function so that users can type what they want.
As well as this variable, we're going to need
some sort of welcome variable that we're going to show
at the very start of our application.
So here, I've created those two variables now,
the next step is to have a loop
that is going to run continuously asking the user
what they want to do with our application
until they enter the number three.
So what we normally do when we want to do something
like that is we use a while loop.
So the first thing is to get the user input.
And we're going to make that equal to the input of menu.
So we're going to ask the user showing them this prompt,
what they want to do,
and while the user input is not equal to the number three,
and remember that the input function
always returns a string,
so we need to compare against the number three as a string.
Then we're going to run our menu
we're gonna do here,
whatever that the user wants to do.
So we'll deal with user input here.
Then at the end of the menu, we will ask the user again,
we'll say user input equal input of menu.
In just a moment, we're going to find
a slightly better way of writing this menu
so that we don't have this duplication.
First of all, we ask the user for their choice.
If it's not three, we start the loop,
we do what they want us to do, be that one or two.
And then we ask them again,
when we ask them again,
we go back to the top of the while loop,
and we check again.
And if it's not three, we run this again.
And then we ask them again, and so on.
To reduce duplication, we can use a new
feature of Python 3.8.
If you're using Python 3.8, that is the walrus operator
or the assignment expression.
The assignment expression allows us to put
this input statement right in here,
like that.
And what this says is, while user input
which should be equal to the input of menu
is not equal to three, we will then run this code here.
Now very important that the walrus operator or
the assignment expression has lower precedence
than the comparison.
So when we write it like this, what we're actually
doing is we're saying, while user input,
which is equal to whether input of menu
is not equal to three,
and then we will run this code.
So what this is really doing is,
it's assigning two user input,
whether the input of menu is not equal to three.
So this will end up being true or false.
It's not actually going to contain the content
of what the user entered.
So we have to put some brackets around it
so that this evaluates first.
And now this is what we wanted.
We first give user input the value of calling input of menu.
And then we compare that against three
and we check that it's not equal to it.
Now when we run this code here,
the user input variable contains what the user typed.
So that's what we wanted.
So again, this is a new feature in python 3.8.
If you're not using Python 3.8, you can use
what we just typed a moment ago instead.
So now that we've got our user input, we can go ahead
and write a series of if statements that we can use
to deal with a different options.
So if the user input is equal to one, then we will add,
so we'll say adding,
and for now, I'm just gonna put this print statement.
And here we're gonna add
some more stuff to it in just a moment.
Otherwise, if the user input is equal to two,
then we're gonna print viewing.
And again, later on, we're going to actually be displaying
the the entries that they have in their entries list.
Otherwise, if it's not one or two,
but we know
it's not three, because if it was three,
we wouldn't be running this code to begin with.
So otherwise, we know that it's not one, two or three,
it must be something else that they've typed
and that is an invalid option, so we're gonna
tell them invalid option, please try again.
So these are the three different branches
of our if statement.
And this is going to allow us to start
thinking about our menu a bit better by seeing it in code.
Of course, remember, we still have to use
these welcome variable there to welcome users
to our application, we're gonna print that at the top
outside of the loop because we only want to print that once.
And that's really our complete code for the user menu.
Next up, we're gonna be adding functionality
to keep track of entries.
But we're not going to use a database just yet.
We're going to add SQLite later on in this project.
First, we're going to develop our programme logic
by using a Python list as a simple database table,
and then we're going to use SQLite later on.
Using a list instead of a database
allows us to quickly develop our programme
without worrying about interacting
with the database just yet.
Once we're happy with all our functions, and how they work,
we can read and write from the database
instead of from the list.
And changing that will be straightforward.
And this way, we only have to worry
about one aspect of software development at a time.
All right, in the next video, we're going to
look at what is SQL and then we're going to come back
to this code and use Python lists as a database.
I'll see you in the next one.