Welcome back. So, Python gets a lot more interesting once we learn how to write code that
doesn't actually execute or output the same thing every single time that we run it.
Now, the first way that we'll learn how to add
some variability into what our code executes,
is by getting input from the user.
So, we'll be able to have different outputs from our code even with
the same program by asking the user for
some input and then doing something with that input.
The way that we'll do that is with the naturally named input function.
So, input asks the user to enter something.
So, for example, in this code on line one we ask the user to input their name,
by calling the input function,
and the input function accepts a prompt as its argument,
and so here we prompt the user,
please enter your name.
Now, the value of this expression of input,
is going to be whatever the user entered.
So, if the user enters Steve,
then the value of this expression is going to be the string Steve.
The value of input is always going to be a string no matter what the user enters.
So, if the user for whatever reason enters 500,
then the value of this expression would be the string 500,
and now n gets assigned either Steve,
500 or whatever string the user entered.
So, when we print out hello n,
then we print out hello and whatever the user entered.
So let's run this code and see what we get.
So, when I hit save and run,
you'll see that I have this prompt here,
and it says please enter your name,
and that's the exact prompt that we passed into input.
So, if I type in Steve,
as my name, then you'll see that my code then prints out hello Steve.
If I save it on my code and I enter something different, so,
let's say that I entered Paul,
then my code is going to output Hello Paul.
I can enter absolutely anything that I want in here.
So, I can enter 99999 and my code is going to print out Hello 999999.
So again, input always gives back a string,
but let's suppose that we wanted a number instead of a string.
The way that we would do that is by casting or
calling one of the functions to convert strings to a number.
So, for example, let's suppose that we wanted to write
some code that was going to convert a number of seconds,
into a number of hours and a number of minutes,
and we want to also compute the remainder
when we actually divide to get the number of hours and minutes.
So, here we prompt the user please enter the number of seconds that you want to convert,
and then we put that into this variable str_seconds.
The next thing that we do,
is we cast that variable str_seconds,
because again input always gives us back a string,
we cast that to be an integer,
and we assign that to be total seconds.
We then compute the number of hours by taking
total seconds and then doing division without remainder,
by 3,600 to convert seconds into hours,
and then we find out how many seconds are still remaining by
taking the remainder of total seconds when it's divided by 3,600.
Then we take those seconds that didn't divide evenly into the number of hours,
and convert them into minutes by again dividing without remainder by 60,
and then we figure out how many seconds are still remaining at the end of this by taking
the remainder when we divide seconds still remaining by 60.
Then in the end we get the number of hours,
minutes and seconds for whatever number of seconds the user input.
So, if I save and run my code,
and then let's suppose that I just want to convert 60 seconds,
then I'll see that this is zero hours,
one minute and zero seconds.
If I convert 6,000 seconds,
then I'll see that this is one hour and 40 minutes.
If I convert 999 seconds,
then I'll see that that's 16 minutes and 39 seconds etc.
So, again the point of this is,
we can use whatever the user entered as an integer by
converting it to an integer by calling
n function or if we wanted to make it a floating point,
we might call it the floating point function.
Now, you might ask what if the user enters something other than an integer?
So, if we're asked to enter the number of seconds that I want to convert,
and I type in let's say my name and I run,
then you'll see that Python wasn't able to
convert Steve into an integer and so we get a runtime error.
Now, let's look at a multiple choice question.
So, this asks, what gets printed when the following statements execute?
So, let's suppose that we ask the user,
please enter your age,
and then the user enters 18.
So, here n gets assigned to the string 18
because remember input always gives us back a string.
It doesn't matter what the user actually entered,
even if they enter 18,
then n gets assigned to the string 18,
and when we print out the type of n,
then we're going to get that n is a string.
That's all for now, until next time.