This is the second activity of this course, and in this activity we are going to ask the user to type
something.
We're going to get this.
So we're going to get the input and we are going to validate the user input okay.
So this is something that's done very often in programming.
And before I give you the challenge I actually need to show you one more function that you didn't know.
It's actually how to get the user input.
So you have seen that you can print something for example with the print function like that okay.
So in this case it's basically an output.
So from the program to the user here the user is going to be the shell okay.
It's whatever you type or that you see on the shell.
So with print you have an output.
Now to get the user to actually type something here and get it into your program, you can use the input
function okay.
So you have print and you have input.
If I run this you will see the program problem is kind of hanging there, and I can just type some text
or some numbers or whatever, and I press enter and you can see now the input function returns inside
the parentheses.
Here I can type a prompt.
Okay.
So that would be a bit better.
So for example enter a number with a colon a space just like that.
So if I run again you see now we have entered a number.
So it's a bit more explicit.
So now I can say for example 56 I press enter.
All right.
Now the thing is we don't do anything with this input.
So I'm going to save this inside.
So because I asked for a number I'm going to save it inside a variable named number is equal to input
okay.
This is first going to ask enter a number to get the input from the shell.
And to store this value inside the number variable.
Still it's not going to print anything.
So then I can print a Uh, number.
So I'm just going to print back what I typed on the terminal.
Let's run this.
Enter a number one, two, three.
So 123.
Let's press enter.
And you can see 123.
So we just print it back the number.
But now there is one thing that you need to know.
And that will be very important for you to complete the challenge.
Is that what you get here, even if you ask for a number, what you get here is not a number.
What you get here with the input function is a string, and that will be saved as a string.
Even if you write number as a variable.
Okay, the name of the variable doesn't really mean anything for the program.
It's still going to be a string.
How to verify that I'm going to write type here type number I'm going to print the type of the data
I get okay.
So for example I put one.
You can see we have a string.
So what we receive here inside the variable number is not one.
It is the string one.
Okay.
So that's going to be a problem if you want to use this variable as an integer number later on.
And so what we can do is we can cast a value to another type okay.
So for example here to make sure that we have an integer going to number we can use the int function.
And I'm going to open parentheses and close parentheses.
Here you can see with the int keyword here that's with syntax highlighting I can cast whatever is in
the parentheses to an integer.
So what's going to happen here is we first have the input.
So we're going to get the data from the shell.
And we're going to cast this into an integer.
Now if I run this and I put the number you can see now we have an integer number okay.
So that would be helpful for our program.
You can also you can cast to int but you can also cast to float.
For example like this.
You can cast to a string with str okay.
So you can cast two different data types here.
We're going to use int.
Okay.
And now I can explain what is going to be the challenge.
So I'm going to remove this print here.
And actually I'm going to change the prompt.
Enter a number between 1 and 100.
So we are asking the user to enter a number that should be between 1 and 100.
Of course the user can type whatever they want here.
And what we want to do in our program is to validate that the number is actually between 1 and 100.
So you already have the first line of the program.
And then you are going to need to create a condition with an if block, maybe with if and else to say
that the number is wrong or the number is correct.
All right.
And I will see you in the next video for the solution.
Make sure you take enough time to think about it to try some things.
Okay, maybe go back to the previous lessons, but make sure that you practice on your own.
That's really important for you to make progress.