How do you accept user input in Python?



Introduction

Importance of user input in python; Most programs are written to solve user problems. For that users need to enter input data. For example, let’s say the government wants to find out whether the child is eligible to enroll in the school or not. If we write a program to answer this question, we need to know the user’s age before we can provide an answer. The program will need to ask the user to enter, or input, their age; once the program has this input, it can compare it to the eligibility age to determine if the child is old enough and then give the result. In such cases, the program has to accept user input.

In this article, we’ll learn how to accept user input so your program can then work with it. To do this, we have to use the input() function.

How the input() function works

The input() function pauses your program and waits for the user to enter some text. Once Python receives the user’s input, it assigns that input to a variable to make it convenient for you to work with.

To make the program more flexible or dynamic, we should make use of the input() function. Input() function takes in a user input.

For example, the following program asks the user to enter a name, then displays the name back to the user:

name = input("Enter your name:")
print(f"Your name is {name}")

When Python runs the first line, the user sees the prompt Enter your name: The program waits while the user enters their response and continues after the user presses enter. The response is assigned to the variable name, then print(name) displays the input back to the user;

Enter your name: Karma
Your name is Karma



Converting input data types

When we use the input() function, Python interprets everything the user enters as a string. But we can also accept the input type based on program requirements. If we are writing a program to do the simple mathematical calculation, our program should accept only numbers, i.e. either integer or float.  Similarly, if we are writing a program to collect the name of the students, our program should accept input as a string type.

We can achieve this by converting the type of data using built-in data type functions such as int(), float(), and string(), and it should be placed before the input() function.  

Example:

age = int(input("Enter your age:"))
print(f"Your age is {age}")

output:

Enter your age:30
Your age is 30

In the above example, the int() function tells Python to treat the input as a numerical value. The int() function then converts a string representation of a number to a numerical representation. Similarly, we can do the same with other data types using its built-in functions.

Importantly, you should also know that you cannot input other types of data once it was mentioned in the input() function. Let’s run the above program again, but this time we will enter the age in the form of text;

Error message:

Enter your age: Thirty
Traceback (most recent call last):
  File "C:/location/main.py” line 1, in <module>
    age = int(input("Enter your age:"))
ValueError: invalid literal for int() with base 10: ' Thirty'

You will get an error message; ValueError: invalid literal for int() with base 10: ‘ Thirty’. This means, once the data type is implicitly mentioned, you can’t input other unmatched data types.

Previous topic: Variables in python

Following things to know when you are accepting different types of user input;

  1. String type accepts all three types of data; int, str, and float.
  2. The float type accepts both integers as well as floating numbers.
  3. Integer type accepts only integer value, not floating numbers.


Official Facebook page