How to take Integer user input in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Take integer user input in Python
  2. Only allow Integer user input in Python

# Take integer user input in Python

To take an integer user input:

  1. Use the input() function to take input from the user.
  2. Use a try/except statement to make sure the input value is an integer.
  3. Use the int() class to convert the string to an integer.
main.py
# ✅ Take user input integer value user_input = int(input('Enter an integer: ')) print(user_input) # ------------------------------------------------ # ✅ Take user input integer value with validation try: user_input = int(input('Enter an integer: ')) print(user_input) except ValueError: print('Enter a valid integer')

user input integer

The code for this article is available on GitHub

We used the input() function to take user input.

The input() function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from the input, converts it to a string and returns the result.

Note that the input function is guaranteed to return a string, even if the user entered an integer.

This is why we used the int() class to convert the value to an integer.

main.py
user_input = int(input('Enter an integer: ')) print(user_input)

If the user enters an invalid integer, we'd get a ValueError.

You can handle the error using a try/except statement.

main.py
try: user_input = int(input('Enter an integer: ')) print(user_input) except ValueError: print('Enter a valid integer')

user input integer with validation

If the user enters an invalid integer, the int() class throws a ValueError which gets handled in the except block.

# Only allow Integer user input in Python

To only allow integer user input:

  1. Use a while True loop to loop until the user enters an integer.
  2. Use the int() class to attempt to convert the value the user entered to an integer.
  3. If the user entered an integer, use the break statement to break out of the loop.
main.py
while True: try: num = int(input('Your favorite integer: ')) print(num) break except ValueError: print('Please enter an integer.')

limit user input to only integers

The code for this article is available on GitHub

We used a while True loop to iterate until the user enters an integer value.

The input() function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from the input, converts it to a string and returns the result.

The input() function always returns a string, even if the user enters an integer.

We used the int() class to try to convert the value the user entered to an integer.

main.py
while True: try: num = int(input('Your favorite integer: ')) print(num) break except ValueError: print('Please enter an integer.')

If the attempt to convert the value fails, a ValueError is raised and the except block runs.

The except block prints the "Please enter an integer" message and the loop re-runs.

If the user enters an integer, we print the integer and break out of the while loop.

The break statement breaks out of the innermost enclosing for or while loop.

# Only allow integer user input in a given range

If you need to make sure the user enters an integer in a given range, use an if statement.

main.py
while True: try: num = int(input('Integer between 1 and 100: ')) print(num) if num < 1 or num > 100: raise ValueError break except ValueError: print('Please enter an integer between 1 and 100.')

only allow integer user input in range

The code for this article is available on GitHub

The code snippet only allows a user to enter an integer between 1 and 100.

If the value is not an integer, is less than 1 or is greater than 100, a ValueError is raised and the loop re-runs.

Once the user enters an integer in the specified range, no ValueError is raised and we break out of the while loop.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.