All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Take Integer Input In Python 3

Last Updated : Mar 11, 2024

How To Take Integer Input In Python 3

In this tutorial we will show you the solution of how to take integer input in python 3, taking input from the user is the very first task we understand while doing programming in python.

We can take input from the user in the form of text, integer and other data types as per our requirement.

Since python has various versions with different functions and features, in this tutorial we’re going to learn that how we can take input of integer data type in python 3 versions.

Step By Step Guide On How To Take Integer Input In Python 3 :-

Python 3 -

  • Python 3.0 is a new version of the python language.
  • Python 3.0 is incompatible with the 2. x line of releases.
  • It can also be termed as Python 3000 or Py3k

Python 3 Features -

  • Per-user site-packages Directory
  • Advanced String Formatting.
  • Implicit and explicit exception chaining
  • New Features And Syntax

Method 1 -

x = int(input("Kindly enter the value as integer: "))
print(x)
  1. First we created a variable named ‘x’
  2. Now, we took input within the x variable, by using input() function.
  3. Within input function, we specified its input data type as integer. x = int(input(""))
  4. After specifying its data type, we just printed that variable ‘x’ by using print function.

Method 2 -

In this method, we’re going to use the concept of exceptional handling in order to detect the error and display a message if the user gives wrong data type value or not integer value in the code.

while True:

    try:
        x = int(input("Enter an integer value: "))
        print("You entered the integer value: ", x)
        break;
    except ValueError:
        print("Wrong value, kindly input only integer value")
  1. First, we used while loop condition by setting its condition as true.
  2. Now, we used try: condition and within it, we gave the condition of taking input as an integer value.
  3. X variable created here, by setting its data type as integer and took input with it.
  4. Here, we printed the value of integer value by referring to the variable’ x’.
  5. After printed the value, when condition stays true, we broke the while loop here by using break; function.
  6. If the condition stays not true, and the user gives input of any other data type, so it’ll throw an error. Which will take the program to the Except value error: function.
  7. Here, we printed the condition and after this, again loop will start from true condition.
  8. By using this condition, we can easily detect an error and can take only integer specific value from the user.

Conclusion :-

In this tutorial, we used two methods, one is basic and second one with error detection to take input from the user.

Taking input from the user as an integer will become a really simple task for you after referring to this article.

I hope this tutorial on how to take integer input in python 3 helps you.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪