Hello guys and welcome back to another class of our course about Python.
So until now we saw many many things and we talked about the if statement.
And right now it's time to put everything in practice.
So what I want to do in this class is really practicing the if statement that we learned before it as
well as the functions and the putting everything together so very simple.
What I propose you guys doing today is a small basic app that will be a conversation between a user
and a person.
So in this case what exactly will the app look like.
So the first thing we will do is we'll tell the rules to our users.
So the rules will simply be answering each question by yes or no.
So if the user says yes to this I will go to the next question.
If the user says no we'll simply print stop then ask the first question the same question.
The third question and the fourth question.
Well the third question because it stops at trick questions.
So basically this is the goal of our app.
So let's start.
So very basically.
But what exactly will start in this case will put everything inside of a phone it of a function.
Because if we don't do this it simply will not work.
All right.
So how exactly this will work.
Well write down firstly def so def is a way to tape them to tell Python that we start our function here.
The name of our function will be question.
So simply name the question.
You write down two points.
All right.
So first thing we'll write down rules.
So the first variable that we will have inside of our of our function will be the variable rules.
In this case we'll ask our user.
You need to answer every question.
So we'll write it down.
You need to answer
every question by yes or no.
Do you under stand.
So you're our user will give us an answer by yes or no.
All right.
So once the user read this he will answer.
So we'll start with our if statement.
So if the variable rules is not equal to yes.
So if it's equal to no.
So if the variable rules is equal to no we want to return stop.
So in this case we'll ask Python to print stuff or will print and we'll ask him to print trade.
So let's say try again
great.
So you can see yes right now our first rule is very simple.
If we if our user returns.
Yes we go to the next question.
If no the the apple stop here.
Well our function will stop here.
This is why we use the return statement in this case will stop our function right here.
If in the case that this is not respected great.
So once this is done it's time to write our second question.
In this case it will be else.
So what's happening in the case that this is not respected.
Well this is respected.
So we'll print next question
great
so we have everything right now.
Next thing that we want to do is write down the first question.
So once again we'll use a variable to write down the questions or in this case we'll have question 1.
So quest 1
and the question 1 will be very simple.
We'll ask our user.
Are you hungry.
So once again we'll write down an input because we want an answer from him.
Are you hungry
and once again we'll have our if statement.
So if the variable quest one
is not equal to yes or is equal to no.
We want to return.
So we will give the answer to our user.
Then let's go for a walk.
In this case
we'll tell this to our user
so once again the question is if this variable right here is not equal to yes we want to return to our
user.
Then let's go for a walk and everything stops here.
So the app will stop there.
In the case that the answer is yes.
So here is not yes.
If the answer is no we return this in the case that the answer is yes.
What we want to do well we simply go to the next question.
So we print else.
Next question.
So what's the next thing.
So right now we'll ask our user.
Do you want to eat at a restaurant.
Since our user is hungry we want to ask him if he wants to eat somewhere.
So once again we'll store our variable right here So write down our input function and ask our user.
Do you want to eat at the race.
Great.
So right now we have our second variable or our second question.
Now it's time to ask the equity question so we'll write down the function if the if statements are.
So ask our user if we'll tell our user if the answer to the question number two or the value of the
variable quest to is not equal to yes
we want Python to return.
Come eat at my place.
Great.
So as you can see if this variable is not equal to yes we want Biden to return come eat at my place
right now we have our two first questions.
So the next thing with our if statement we're here we have what's happening in the case that this is
true but in the case that this is false in the case that it's equal to yes what what exactly from Biden
we want from him to print us.
Next question.
Once again the print next question is.
Well we are not in the obligation to use it but once again it's it could be a good thing to use it right.
We could work without the L statement here.
We can just go with the if statement and then jump directly to the quest number two but once again we
want that we'll have it.
It's gonna be more let's say more cool.
I find it more cool to write at them.
Right.
So next thing we go to the last question that we'll have.
So once again we'll have our last question in this case what we want to do we'll do the exact same thing.
We store our variable which will be quest treat.
So question number three and in this case the last question will be do you.
What do you want to eat a pizza to input once again.
And we ask our user do you want to eat a pizza
so in this case if our user
tells us that he wants to eat it he wants to eat it pizza.
Tell him let's go eat a pizza in the case that he tells us that he don't want to eat a pizza we'll tell
him let's go eat a burger.
So sorry not no.
Yes.
All right.
So in the case that our user tell us no or the answer is not equal to yes we will return.
Let's go eat a burger then
go eat a burger then great.
And in the case that the answer is yes we will return fancy and print.
Let us go at the let's go eat pizza.
So right now we have absolutely everything that we need to be able to run our app.
So let me just summarize everything that we just wrote.
So once again we started with Def def means the opening of a function.
So we write down our function the name of the function which has questions and what is inside of our
function.
Very simple.
We have the rules so input.
You need to answer every question by yes or no the first.
So right now we have our variable here rules.
So if the variable rules is equal to yes is not equal to yes sorry we return try again if it's equal
to yes we return next question and we go to the next question.
So in this case next question will be are you hungry.
If are you hungry.
Is not equal to yes we return then let's go for a walk.
If it's equal to yes we return.
Next question and we do the same thing for question number two in question number three so let's run
up and see what it looks like.
So as you can see if we just click it doesn't work.
So the way that we have to run our app is very simple we write down a question right here.
So we write down the name of our function there.
We run our up once again.
Let's do this.
So first question you need to answer every question by yes or no do you understand.
So let's say we write down no.
As you can see.
Try again and it stops.
If we run our app and says and say yes.
Next question.
Are you hungry.
So in this case yes I am.
Next question Do you want to eat at a restaurant.
Let's say I write the note.
Come eat at my place.
And this is where the app stops.
So once again you can write to points everywhere just to make it more beautiful and more presentable.
But besides of that you see guys how all this works.
So it's very very simple.
So we run the app another time just to be sure at 100 percent.
Everything works perfectly.
So let's run the app.
So you need to answer every questions we understand.
Yes I understand.
Are you hungry.
So in this case let's say yes.
Do you want to eat at a restaurant.
Yes.
Do you want to eat a pizza.
Let's say.
No let's go eat a burger then.
So as you can see every time that I answer yes it goes.
Next question.
If I answer No everything stops here.
So this is the goal of this app once again we just ask questions to our users.
And as you can see it's something very very basic.
Once again it puts everything that you guys just learned into practice.
So I hope you guys right now understand much much better the if statement and how to use it you understand
functions and everything because it's a very very important part of Python.
So right now wrote a very simple code but you'll see the more will advance in the course the more you'll
learn things so you'll need to remember.
Absolutely.
Well you'll need to remember everything.
If you want to be good in programming so I'll suggest you to practice with me every time that we do
small exercises like this.
So that's it for the slides guys.
And see in our next class.