Okay before explaining the while loop, I'd like to introduce you user input.
User input is a feature that allows your script to interact with the user and
with interaction I mean when the user executes the script so like when we call
the script here, the script will ask the user to enter some input. Let's say they
will enter a number and the script will get this number and we process it, will
do some operations depending on what we have written inside the script and then
it will return some results back to the user.
And the user input feature is implemented through the input function
as simple as this input, and here you can write some message that will be shown to
the user. Let's say what planet are you from?
Just like that. Save the script and then go to your command line and execute.
This is script6 and the program is asking us to enter some input. Let's say
Earth. That's it. Here nothing happened because we didn't
do any other thing here rather than just asking the user to input some value so
let's do something with that value, but first of all or we need to store that
value inside the variable. That would be a good practice. Let's say planet equals
to the value that the user will input, so the planet will get the value that the
user will enter here. Then we can do something with the value.
Let's say we want to print that value. Planet. That's it. So let's execute again.
Mars this time. So the program prints out Mars. Okay, printing is good but let's go a
bit more advanced than that, so I'll go to my script2 and what I have here is a
function. Let me delete this, so we had the function here which we wrote before in
the course and this function gets the currency rate as input and amount of
euros which you want to convert to dollars. So the function calculates the
dollars given the euros and rate and it returns the dollars. What we will do now
is get a value for the rate and a value for euros from the user, so we'll get
those values here and then we will execute the function given those values.
For that we need two inputs here. Let's say R as a variable input.
Enter rate and once more enter euros. And this would be e. And then these
values would go here, so r for the rate and e for the euros, so we execute the
script. Python reads the function definition then it asks the user to
enter the rate. The user enters the rate. Then the script goes on. Python ask the
user to enter the euros. The user enters the euros and then those values are tests
to the currency converter function. Now I expect to get an error here, but I still
want to execute it so that you see what error you will get and that's how you
understand things better, so let's execute script number two yeah. This is
not the error I was talking about. This is because I forgot to save the script, so
let me execute it again. And so we are now asked to enter the rate. Let's say 100.
Yeah and euros let's say 3 euros. Why not?
And here is the error I was talking about, and Python is basically saying
that it cannot multiply strings. Why is that? Well that is because when the user
enters the numbers here as we did, the input function reads those values as
strings, so it doesn't immediately read them as numbers as we would probably
expect, but there is a solution to that. Once we get those strings here in the r
and e variable, we could convert them to a float. We would want to apply the float
function to each of these variables, so before these values will be passed to
the currency converter function for execution, the values of these variables
would first be converted to a float data type and lastly we would want to
print out the output that this function generates. That's it. Click save and
then go to the command line. And script2, enter rate. Let's say
four euros 102 this time, and so we get 400. So that was how you can make your
script interactive, so that the script can get the user input, process it and
then returns an output. In the next lecture I'll be incorporating the user input
function, while teaching you the while loop, so see you in the next lecture.