Hello and welcome to this video.
When you are programming there will come a time when you want your program to be more interactive and
capture some information from users in Python.
You can achieve this by using a special function called the input function.
This function prompts the user for some information.
What you can then do is capture that information that the user enters and store that information into
a variable and then you can interact with the information the way you want.
I'm going to create a new python file to show you some examples of how to capture your information from
users.
So I'm just going to right click on my main project directory right click and go and you click on python
file.
I'm just going to call these inputs and click OK.
So it's now created the new file called input dot P Y so to capture information from a user you need
to use the input function and what you can do.
You can store the information that you do Capshaw into a variable.
So let's say I create a variable called person and I'm going to set it to increase to an input function.
Now inside that input function I'm just going to add some text that will prompt the user what to do.
I just say enter your name.
So depending on what type of information you're trying to get from the user you can have as many input
functions as required.
For example you can have an input function that will prompt users for their age and other kinds of information
and then store that information into a variable.
So once we have captured that information we may want to output that on the screen and we can do that
using the print function.
So inside the print function we can just add a text.
So what this would do basically once the user gets the prompt to enter the name and then this will print
Hello plus whatever name they have entered.
So I'm just going to save my file and then I'll play this file slightly complete I you can see here
is now giving me the prompt to enter some information.
So I'm gonna send out some information I'm just going to see blue line and when I press enter it's going
to say hello.
Blue line because blue line is the value I have supplied for this variable called person.
So I press enter.
I can see here it says hello.
Blue Line.
So this is just a simple way to capture a user's input so you can have different types of input depending
on the type of data or information you want to get from the user.
You can also capture more Numeric Input from a user.
So let's create some variables.
Well if I created variable X and I'll give it a a value which will be close to an input function.
So I say input because I want to capture the information from the user.
So inside the input function I'm just going to enter a text that says enter and then how create another
variable I'll make this on y and I'll set it again to an input because I want to capture some information
from the user.
Again I I'll say and.
Okay.
And then I can have another variable cause Z and I'll set these two equals two X plus why.
Okay.
And then I can do a print statement.
Print Z.
So when I play this file I want you to watch the outcome carefully.
It's not gonna be the results you expect.
So you would expect an addition to take place.
But what's going to happen is you're going to get a concatenation concatenation is when you add two
strings together.
So let me click on the play button you can see says enter your name.
It's referring to this first print statement on line two.
So I just say and press enter.
All right.
So says hello.
Blue Line.
Now he's gone on to this print statement here on line number eight.
All right so he's basically responding to this.
Says enter a number.
So and number eight a press Enter is asking me to enter another number.
Don't forget I've got two input functions or I in turn number seven.
Now if I press enter you would expect the result to be 15 boys not he's gonna say 87 XY.
It says 87.
And the reason it has said 87 is because it has added the values I have provided.
Together is treated them as a string.
So he thinks they are text and just added them together.
When you add strings together is known as concatenation.
So what we need to do here we need to do some casting so that this value that it captures is converted
in two and a number or an integer.
But in order to convert the values that has been captured from the user in to a number value an Integer
i need to do some casting.
So what I'm going to do here in this variable Z.
This is where I'm going to do the casting.
So what I'm going to do I'm going to wrap this round a print statements.
I'm going to see print and I'm going to wrap that result but I need to include a special function called
The End.
This is what will convert the value that I can show from X and I'll do the same here.
Add this function called INT.
It will convert whatever value that is being captured from Y as well.
All right.
So that will then give us a proper addition.
So I'm going to modify the code.
I'm going to comment out.
Line number eight.
We will not need that anymore since we have ramped the valley of the variable Z inside this print function.
I'm also going to comment out.
Line 1 on line two.
I don't really want that line.
Those line of code to run.
So I just want to keep them simple so we can focus on this area.
So what we will not get.
Now we'll get a prompt to enter a number we enter a number we enter a second number and then in this
verbose Z here to print the number that was entered for X but then converted because when it's entered
the input level it's a string.
So this end here is going to do some casting by converting the captured number into a whole number is
converted from string to number.
It does the same with the Y.
So let me just save my code and then play the code again so we can play.
Now you can see here is asking me to enter a number.
So I say eight and a number eight for example a press Enter says enter another number.
Say I press for five press Enter now it should give me the correct result which is twelve.
Okay.
And the reason is giving me the correct result is because I have done some casting here is in the int
function it converts the value that was entered for the x variable into a whole number.
He also converts the value that was entered for the Y variable into a whole number because any value
entered using just the input function is treated as a string or a text.
All right.
So we have to cost by changing the data type of what was entered.
That's why we were able to get the correct result.
Of twelve.
So that's basically how you can capture your input or information from a user is very useful.
So depending on what type of information you want your program to capture you can use that to do different
things in your program in order to create some kind of interaction with the user.
So that's basically how to capture user information by using the input function.
Thank you for watching and bye for now.