How to take integer user input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 24, 2022

banner

Photo from Unsplash

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

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 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.

You can use a while loop if you only want to allow the user to enter valid integers.

main.py
num = 0 while True: try: num = int(input("Enter your favorite integer: ")) except ValueError: print("Please enter a valid integer") continue else: print(f'You entered: {num}') break

only allow integer user input

We used a while loop to only allow the user to enter an integer value.

The code snippet keeps prompting the user for input until they enter a valid integer.

If the code in the try block raises a ValueError, the except block runs, where we use the continue statement to continue to the next iteration.

If the user enters a valid integer, the try block completes successfully and then the else block runs where we use the break statement to exit out of the while loop.

The continue statement continues with the next iteration of the loop.

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

When validating user input in a while loop, we use the continue statement when the input is invalid, e.g. in an except block or an if statement.

If the input is valid, we use the break statement to exit out of the while loop.

You can use the same approach if you only want to accept integer user input in a specific range.

main.py
num = 0 while True: try: num = int(input("Enter an integer 1-10: ")) except ValueError: print("Please enter a valid integer 1-10") continue if num >= 1 and num <= 10: print(f'You entered: {num}') break else: print('The integer must be in the range 1-10')

only allow integer user input in range

We used a while loop to iterate until the provided input value is in a specified range.

If the try block completes successfully, then the user entered an integer.

The if statement checks if the integer is in the range 1-10 and if the condition is met, we break out of the while loop.

If the integer is not in the specified range, the else block runs and prints a message.

If the user didn't enter an integer, the except block runs, where we use the continue statement to prompt the user again.

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.