[No audio]
As you know from the previous lectures, we have
defined this function and here we are calling it.
So it checks if the given temperature is greater
than 7 and it returns warm if that is
true, and cold if the temperature is equal to
7 or lesser than 7, that's fine.
But what is the use of this
function in a real life setting?
Like, who is giving this input?
You as a programmer, you are writing this function for
someone else so that they can use it, but you
don't expect them to edit the Python file,
like to have an editor like
Visual Studio Code or other editors.
And then you cannot just tell them to go to
line 7 and change the value and then execute that
code to get different outputs depending on the value.
So how about prompting the user to enter a value
and then the user types in the value, and then
we get that value and process it in our function.
In that case, we get that value and
put it in here in the function call.
Let's do that.
So we've got a function there.
We want to get a value from the user.
To get a value from the
user, you use the input function.
The input function gets an argument and that is the message
you want to show to the user on the command line.
For example, Enter temperature.
[Author typing]
Maybe put a colon there if you like.
You can write whatever you like.
Let me save this script and execute it.
You see now I execute it,
and this input function, what it does is
it freezes the execution of a program and
waits for user input on the command line.
So if I put input something here, like seven,
in this case, what's going to happen is that
this expression here will be equal to seven.
However, because we didn't do anything here, nothing
will happen if we print out this value,
[Author typing]
for example, seven, you'll get printed out
the value of this, which is 7.
So there you can put anything you want, Hi
there, and you get Hi there, printed out.
What I would suggest is, instead of printing this
out, you'd first want to put it in a
variable that would make it more readable.
So user_input,
and then you can do whatever you
want with that variable, like lower it.
So to print out the lowercase version of that Hi Hi.
So you get hi hi with lowercase letters.
Okay, back to our function.
Let me now try to print out the weather_condition
function output with a value of, well, user_input,
whatever the user enters as input.
So I'll execute the program
now, the program will ask me with Enter temperature.
The program will ask me to enter a temperature.
So the execution is frozen at this point.
This line has not been executed yet.
It will be executed after we give
a value here and press Enter.
So let me write down 6.
Let's see what we're going to get.
This time we get an error.
Let's read the error carefully.
It's about file basics.py, which is our file.
Okay, the error happened at first at this line here.
So here print(weather_condition(user_input)).
When Python tried to execute this
line, it couldn't do it.
And why?
Well, you have to follow the error.
So steel line basics.py, line 2 at this line.
So when Python tried to execute this line, that
line called some other things in the script and
the error was in this line, line two.
So when the function call tried to call
the function, it tried to execute this line.
So if temperature, it tried to compare 5 with 7.
And then it got this error when
it tried to compare 5 with 7.
So when it tried to execute this line, in
other words, see what it says TypeError.
This operator is not supported between
instances of str and int.
So str and int, which means this was
a string and this was an int.
So our user_input was 7, was passed
in here, and that was compared with 7.
It seems that our input, the user_input was a string.
And that is true. And that was the point I was trying to make here.
Because whatever input the user writes in
here, even if it's a number, Python
will actually convert it to a string.
So 7 in fact, will be converted to a
string like with quotes or double quotes inside Python.
But that has an easy fix.
What you can do is you can convert this
[Author typing]
value, which
is a string seven, you can convert it into a float.
Be careful with parentheses.
So float is a function.
It has an open parentheses in here and
it has a closing parentheses in here.
input is also a function with
its own opening and closing parentheses.
So now if I do that, Enter temperature
5, I'm going to get the correct answer.
And let me be transparent with you.
Let's again input, get user_input.
Enter some input.
Let me now print out the type of user_ input.
Execute, enter 6.
You'll see that 6 actually is a string.
That's why I am converting it into a float.
So that 6 this time will be
a float actually, which looks like this.
You can print out the actual number here, the
actual converted number, the float version of the number.
Execute again, 6.
So 6.0, you can also use Int here if you like.
[Author typing]
6, so you'll get 6, which is equal to 6.0, of course.
But float would be a better choice
because, I can show you why.
If you enter 6.3, for example, int will
not be able to convert it because that
is like doing int("6.3").
So int will get confused.
It will not know how to convert that into an integer.
It does, however, converts 6 if it was
a simple 6 without a decimal point, float
will convert both sorry, both 6 and 6.3.
And that's how you use user input in Python.
[Outro sound]