Accept user input only if in range using Python

avatar

Borislav Hadzhiev

Last updated: Aug 23, 2022

banner

Photo from Unsplash

Accept user input only if in range using Python #

To accept user input only if in a range:

  1. Use a while loop to iterate until the input value is in range.
  2. On each iteration, check if the value is in the specified range.
  3. If the condition is met, use a break statement to break out of the loop.
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')

accept user input only if 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.

main.py
num = 5 if num >= 1 and num <= 10: # 👇️ this runs print('The number is in the range 1-10') print(num in range(1, 11)) # 👉️ True # 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list(range(1, 11)))

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

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')

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.

The continue statement continues with the next iteration of the 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 also use the in operator and the range() class to check if the user input value is in a 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 # 👇️ now using in operator if num in range(1, 11): print(f'You entered: {num}') break else: print('The integer must be in the range 1-10')

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

main.py
r = list(range(1, 11)) print(r) # 👉️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(5 in r) # 👉️ True print(13 in r) # 👉️ False

The range class is commonly used for looping a specific number of times in for loops and takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

main.py
print(list(range(5))) # 👉️ [0, 1, 2, 3, 4] print(list(range(1, 5))) # 👉️ [1, 2, 3, 4]
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.