Hey, in this video, you will learn how to have your program take decisions.
In other words, you can make your program more intelligent so that it responds to different ways depending
on the user inputs.
So exactly what we'll be doing is we will allow the user to decide what they want to do.
So whether they want to add a new to do or to see the existing list of two zoos, because currently
the way the program works is that the user can enter it to do and they see the list of to dos, they
enter another to do.
They see the list again.
So how about not showing the list by default, but only showing that if the user wants to see it, we
can implement that using the match case statement.
Now, Match Case is only available in Python 3.10 or newer versions of Python.
Therefore, for this to work you need to use at least Python 3.10.
Right.
So how do we implement this decision making?
Well, first, we want to ask the user if they want to add a new to do or if they want to see the to
dos.
So what I'm thinking is of changing the message here to type, add or show.
So that's just a string.
The string goes in here.
I would simplify this actually, and just put the string directly here so we don't have so many layers.
Both versions are fine.
But I do think this is a bit more readable.
So by looking at this, you understand that you are asking the user for input.
Since this is an input function and you see what message you are showing to the user.
Now the user is expected to enter, add or show and add or show will be stored in this variable.
So to do will be equal to either add or show.
But I want to change the name of this variable now to make it more meaningful.
User action would make more sense.
And then we say.
Match user action.
A column and press enter and you'll see that points are automatically indents the next line.
So this match is a block match.
Case statement is a block just like while through so much user action case and.
To do is equal to input.
Enter a to do.
Let me complete this and explain it to you.
What I'm doing is show.
Print to dos.
And this will go down here.
And this will be deleted.
And that's the complete code for this video.
Now, let me explain to you what this means.
So we start here, match user action.
This variable, as I said, is this one here.
It will be either add or show.
So we're trying to check here the value of that variable.
We're trying to match that variable.
So if the value of that variable is ad, so if the case is ad, then what we do is we ask the user for
A to do so, they will be prompted again to enter some inputs.
They will enter it to do such as clean the bag.
That to do will be appended to the list.
To that list.
And that's it.
If the user action was show, So if the case is true, then we print out to dos.
So let me execute this.
And so we get this user prompt type ad or show.
If I press, show and press enter, we're going to get an empty list because currently there are no
to dos.
So if I press add, then I get this.
Prompt to enter a new to do.
Clean the bag, press enter again we get type, add or show.
Why do we get again?
Type and or show?
Well, because when we were asked to enter a to do, we were at this point.
And we entered it to do this was executed.
And so.
The loop will be repeated again, so it will go and ask for type at or show again.
So a user enters, add for example, add a stored in the user action variable.
The match case again checks.
If it's ads, it does those things.
If it's show, it does that thing, and then goes back again to the first line of the wild through loop,
which is this line here.
And so the loop iterates over and over again.
Now, how about adding one more option here?
Type at show or exit?
So the user can also enter exits.
In that case, we want to add another case or case exit.
We want to exit the program.
At this point, the way we do that is using the break statement statement.
So it just writes break there and that will break the while loop.
And so since there are no more lines of code down here.
The program will end or you can add here print by and let's see how this works.
So we run the script.
Type ads show or exit ad.
Prepared the highly copter type and show or exit exits.
We get by and the process, the program finishes its execution and that is the complete code for this
video.
Thank you.
I'll see you later.