Only accept Numbers as user input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 18, 2022

banner

Photo from Unsplash

Only accept Numbers as user input in Python #

To only accept numbers as user input:

  1. Use a while True loop to loop until the user enters a number.
  2. Use the float() class to attempt to convert the value to a floating-point number.
  3. If the user entered a number, use the break statement to break out of the loop.
main.py
while True: try: # 👇️ use int() instead of float # if you only accept integers num = float(input('Your favorite number: ')) print(num) break except ValueError: print('Please enter a number.')

only allow numbers as user input

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 input, converts it to a string and returns the result.

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

We used the float() class to attempt to convert the value the user entered to a floating-point number.

main.py
while True: try: # 👇️ use int() instead of float # if you only accept integers num = float(input('Your favorite number: ')) print(num) break except ValueError: print('Please enter a number.')

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

The except block prints the "Please enter a number" message and the loop re-runs.

If the user enters a number, we print the number and break out of the while loop.

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

If you only accept integer values, use the int() class instead.

main.py
while True: try: # 👇️ use int() instead of float # if you only accept integers num = int(input('Your favorite number: ')) print(num) break except ValueError: print('Please enter a number.')

Note that if you use the int() class to try to convert a string that stores a floating-point number to an integer, a ValueError is raised.

main.py
# ⛔️ ValueError: invalid literal for int() with base 10: '5.5' print(int('5.5')) # ✅ works print(int(5.5)) # 👉️ 5.5

The float() class is much more forgiving.

main.py
print(float('5.5')) # 👉️ 5.5 print(float('5')) # 👉️ 5.0

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

main.py
while True: try: num = float(input('Enter a number between 1 and 5: ')) print(num) if num < 1 or num > 5: raise ValueError break except ValueError: print('Please enter a number between 1 and 5.')

only accept number user input in range

The code snippet only allows the user to enter a number between 1 and 5.

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

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

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.