This is the solution for the activity number two, where you have to validate the user input.
And I already gave to you.
Well the function input.
So you can get the user input.
And also how to cast it into an integer.
Just one thing I'm going to run this.
And what I can provide a number any number is going to work.
But let's say I provide a string.
You will see that we get an error because we cannot cast a string to an integer.
So already with this program, we are sure that whatever is after line one is going to be an integer
number.
And now I'm just going to save this as activity two dot p y.
So let's save the file.
Okay.
So you will be able to download this file as well.
And let's write our condition here.
So we have a number.
How do we validate that it's between 1 and 100.
So there are actually two ways to do that.
You can either check if it's between 1 and 100.
Or you can check if it's actually lower than zero or greater than 100.
Okay.
When you want to validate a user input, you can check if it's correct or you can check if it's wrong.
And usually we're going to check if it's wrong first.
So there are many examples of that.
For example, if you if you go online, you create a new account in a website and you need to set up
a password that's at least, for example, 10 or 12 characters.
Usually what you will see is that if the password is less than 12 characters, you will get a message
saying the password needs to be at least 12 characters.
So you get an error.
You get a message to tell you that the length of the password is wrong, but they don't really tell
you anything.
If the password is 14 or 15 or 20 characters.
Okay, the important thing is just to make sure that the length of the password is not wrong.
Okay, so you can see very often to validate that the user input is correct, we actually verify that
it is not wrong okay.
And that's what we're going to do here.
I'm going to do if.
So we want the number to be minimum one.
So I can say that if the number is lower or equal than zero, or if the number is greater strictly than
100, then the number is wrong.
Okay.
So once again if the number you provide is zero or less, we are already out of the valid range.
Then if the number is greater, so strictly greater than 100 because 100 is valid, then we are also
out of the range of accepted values.
Okay.
So with the combination of those two conditions here with the Or keyword, we check if this is true.
So for example if number is minus one then well this is going to be true.
And then if the number is well greater than zero.
But if it's also greater than 100 this is going to be true.
So we can print wrong number.
So wrong number.
And actually let's print.
Let's print the number.
So.
To concatenate two strings you can do plus okay.
And number this is going to give us an error.
I'm just going to come back in a minute.
And just to note here is I have used the lower or equal than zero.
I could have used strictly lower than one.
Okay.
You would have also worked here for integers.
I'm just going to keep this one.
And so let's actually try this.
Let's try this with a wrong number.
So enter a number between 1 and 100.
Let's say I put 200.
Let's see what we have.
Well we have an error because you can see on line four you can only concatenate string with string not
integers.
Why we have this error.
Because now number is an integer.
We just cast it to an integer.
Now it says that you can only concatenate string with string.
So this is a string.
This is an integer.
So that's that's a problem.
How do we fix this.
Well we can cast this integer to a string.
So we do str number.
So basically we cast back the number to a string and make sure you add the correct amount of parentheses.
All right.
So we have a string plus a string.
Let's run again.
And let's put 200 again.
You can see wrong number 200.
Now if I put a correct number.
So let's say one one should work.
You can see we don't print anything.
So maybe we could say that it's correct.
So how do we do this.
We just create an else block print correct number.
You could also print the number again if you want.
Now let's run again.
So enter a number between 1 and 101.
You can see the correct number.
We have validated the number.
Let's run again with.
Let's test the edge cases.
So 100 100 is the correct number.
Okay.
Now for example if I put zero or if I put minus one you see wrong number.
Great.
And we have successfully validated the user input.
And as an additional challenge.
So I'm not going to give you the solution here.
But you could do the opposite.
So instead of checking if it's wrong you will check if it's true.
And for this you will also need to combine two conditions with the statement and keyword.
Okay.
And you can easily test actually if your code is working, because when you put a number between 1 and
100, you should see that it's a correct number.
All right.
But that's if you want to do additional practice with conditions then you can continue with loops.
And we are getting close to the end for the Python basics for this course.