-: Hi guys, and welcome back!
In this video, we'll learn about getting user input
so that we can get some data
from our users and use it in our programmes.
Let's start off by asking the user for their name,
so we can print it out.
We will say name equals, and again,
this is us defining a variable.
Now, we have to tell the variable what its
value is going to be.
So what we're gonna do here is we're going to
ask the user for their name.
And the way we do that in Python is we type,
input, then a pair of brackets,
and inside the brackets we are gonna type a question
that we'll ask the user or a prompt.
So here, goes a string where we put enter your name.
So here, we're calling another function.
Just like the print function.
But in this case,
this function here will print this prompt out.
And then, it will wait for the user to type and press enter.
Then, we're gonna print out the name.
So, I've run this code here,
and you can see that we get asked to enter our name.
I'm gonna type here, Rolf.
And then, Rolf comes back out.
So, this is how you can ask the user for their name,
or indeed, any other piece of data.
And then, use it in your programmes.
If you wanted to ask the user for a value
that you want to do mathematics on,
then there is something important to take into account
when you're coding in Python.
And that is that the input function always
gives you back a string.
So, this input will ask the user for a value,
and it will give you a string.
Now, remember that you can't do mathematics on strings
in the same way that you can with numbers.
So, if you wanted to calculate the amount
of square metres that is the equivalent
to the size of their house in square feet,
you would do size input, divided by ten point eight.
That's more or less the right number.
And then, you can print square metres.
But, remember that you can't do maths that well.
So, if I save this,
and then I go ahead and run it,
and I ask for example my size is 500 square feet,
you're gonna get an error.
And you can see here that you've got a type error,
and it says,
unsupported operand types for the division
string and float.
So, what this is saying basically is that
we've tried dividing a string by a float,
and you can't do that.
So, we need to convert this into a number
instead of a string.
So, what we've got to do is we have to
first turn this size input into a number.
So, what I'm gonna do is I'm gonna define
a variable called square feet,
and I'm gonna say "int" of size input.
And what this is doing is it's
doing something like what we had before.
We are passing size input into another function,
which is this int.
And that is going to try to turn our string
into an integer, a whole number.
So that we can then do maths on it.
Notice that here in our square metres line,
we're still using size input,
which is our string.
If we wanna make sure that we use the number,
we must use this square feet variable
that we've defined on line two.
Otherwise, that means we're still using the string.
Let's save this and then run it.
So here, we've got this app running,
and I'm gonna type in 500.
And now we get 46.3.
So here's a small challenge for you.
Try to print something a little bit nicer,
rather than just this square metres.
For example, you could print something like,
this number of square feet is equal to this number
of square metres.
I'd recommend pausing the video and giving it a go.
But, I'll try and do it right now as well.
We're gonna use an "f" string.
So I will type the f-key,
and then the double quotation marks,
and inside it,
I'm gonna use my square feet variable,
and type square feet, square feet is square metres,
square metres.
Notice that this here is the variable
that we wanna put the value of inside our string.
So, we still have to say what that variable means.
So, for example, 500 square feet is 46 square metres.
So, if we run this again,
you'll see that as soon I type a 500,
we get a nicer input.
500 square feet is 46 square metres.
If you want to format a floating point number,
so that you only get a certain number of digits at the end,
you can do point two f, for example.
So here, what we've got is a slightly more obscure
piece of syntax.
We've got square metres colon point two f.
And what that does,
is it will try to format this square metres
with two decimal places only.
This is what this piece of syntax means.
So, I'm gonna save that,
run it again,
and now you see we get 46.30 square metres.
If you want to learn more about this formatting syntax,
I'm gonna leave a link in the resources section
of this lecture,
or on the description below,
so that you can have a read of that.
Well, that's everything for this video.
We've learned how to get user input,
and how to turn it into a number,
in case we need to do maths on it.
Thanks for joining me in this one,
and I'll see you in the next one.