TAKING USER INPUT IN PYTHON

Taking input in Python

User input is a very important thing in any programming language. Getting the user input proves to be very crucial at times when there is situation in that we need some data from the user. In many different situations, even as a user, you would need to enter some user-specific input into the software. For example, if you visit an ATM machine to withdrawal of money, you will go through some user inputs, like user pin, action to do, and the amount to withdraw. Similar to this, there can be many other situations, in our programming, where we would need to have some sort of input from the user.

So, now we are going to explore how we can have user input in python.

Well, taking user input in python is very easy. There is a built-in function in python, using which, we can take the user input. The function name is also input. So, we will also have a look at a simple program, demonstrating a user input in python.

The input function returns some value, and we would assign that to some variable as well. Let’s have a simple, and sample program to demonstrate the user input, using the input function.

In the above program, we have made use of the input function, to get the user input in our python program. As you can see, message whatever we want to give at the time of user input, we are giving it as an argument to the input function. So, now, let’s have a look at the output of the above program –

What is your name?: GyaniPandit
Hello GyaniPandit

As you can see in the output, the string GyaniPandit was provided as the user input, and also the user input is assigned to some variable. So, in this way, we can get the user input in our python program, using the input function.

Now, the thing is that the input function is always going to return a string, even if the user has input some number, or even some boolean value. We can verify this by checking the type of the value from the user input. Let’s have a look at this now.

As you can see, we are trying to get a number as user input. But here, the input function always returns a string. So, even if the user enters some number, the input function returns it as a string. Have a look at the output of the above program.

Please enter a number:12
<class ‘str’>

As you can see, the output tells us that the object is of type str, which means that even if the user is entering some number, the input function is returning the string. So, how we can fix this? Well, here, we have to convert the input to the required type. For example, if we want the above input as an int type, we need to convert it to the int type. So, we need to do typecasting.

So, let’s have a look at a program, where we need an integer input from the user, so we are typecasting it.

As you can see in the above program, we are trying to get a number as user input, so, we are typecasting it to an integer. So, from the input function, we are going to get a string, and we need to type cast it to the required type, as in the above case, we need an integer, and we did so.

Remember that whatever message you need to prompt the user for the input, you can directly have it as an argument here, as you can see from the above examples.

Also remember that here, we are trying to typecast the data to the required datatype. But here, since we are typecasting data, it is a possibility, that we get into an error if we are trying to typecast some invalid data. For example, in the above program, if we are trying to use the same program and we input the string ‘GyaniPandit’, then we are going to get into an error, which says something like this –

ValueError: invalid literal for int() with base 10: ‘GyaniPandit’.

So, we need to keep in mind such situations, when we are dealing with user input.

Very often, we need to have some user input in our programs, and in such situations, the input function is there for us. You can practice more, for different kind of data as user input, and as we move forward, we will use and explore it more.