Taking user inputs in programming language is very essential feature, but what if the user is not providing a correct input? But correct input is essential for the program. In this python tutorial, we will learn how to continuously ask the user for input until they give a valid response.
Table Of Contents
input() function in Python
In Python programming language, we use input() function to take a user input. By default it returns every input as type
str, but we can type cast the user input into our desired data type.
Below is an Example.
# taking a user input and typecasting it into type int. age = int(input("Enter Your age: ")) print(age) # type() will print the data type of var age. print(type(age))
Enter Your age: 18 18 <class 'int'>
In the above code, by using the input() function we have prompted a user input and typecast it into class int. Also before Python version 2.7, raw_input() was used in place of input() function to take user input.
Now we will learn about some methods, using which we can continuously ask the user for input until they give a valid response.
Method 1: Using While loop
First method we can use to Continuously ask the user for input until they give a valid response is while loop. In this method we will loop until condition evaluates to True. If the user input satisfies the criteria, then loop will break using the
break keyword and comes out of the loop, till then the loop continues.
See the example code below:
# Loops until true while True: # prompting user input guess = int(input("Guess the number:")) # created a range. if 25 >= guess >= 20: print("Correct") # if matches comes out of the loop. break # code continues. else: print("Try again")
Guess the number:30 Try again Guess the number:27 Try again Guess the number:19 Try again Guess the number:20 Correct
In the above example, while loop and if-else block Continuously asked for the user to provide a valid input until the criteria matches.
Method 2 : Using lambda function.
Another method we can use to Continuously ask the user for input until they give a valid response is the lambda function. It is an anonymous function starting with lambda keyword instead of
def keyword. It takes n number of arguments but returns only one evaluated expression.
See an example below:
# lambda function to Continuously ask for user input. guess = lambda num: (num.isdigit() and \ int(num) >= 20 and \ ((int(num) <= 25 and "Correct") )) or \ guess(input("Not correct, Enter again: ")) print(guess(input("Guess the number: ")))
Guess the number: 2 Not correct, Enter again: 7 Not correct, Enter again: 26 Not correct, Enter again: 19 Not correct, Enter again: 25 Correct
In the above example, we have used the lambda function to continuously ask the user for input until they give a valid response.
Method 3 : Using PyIntPlus module
Another method that we can use to continuously ask the user for input until they give a valid response is in the
PyIntPlus module, which is for re-attempt user prompts with max try limit and many other useful features. Type
pip install pyinputplus in cmd to install
PyIntPlus module. Here we will use the
inputInt() function to take integer inputs. See an example code below:
# importing PyIntPlus import pyinputplus as pyip # prompting user input. guess = pyip.inputInt(prompt="Guess the number: ",greaterThan=20,lessThan=25)
Guess the number: 1 Number must be greater than 20. Guess the number: 2 Number must be greater than 20. Guess the number: 24
In the above code, by using the
PyIntPlus module you can Continuously ask the user for input until they give a valid response. We have used
inputInt() function with
lessThan parameters which specifies external boundaries. There are many other essential parameters like: limit the number of tries as well as time, max and min boundation.
So In this Python tutorial, we learned about various methods using which you can continuously ask the user for input until they give a valid response. We learned about three different methods and also about input() function. Using input with while loop can do our work in most of the cases, but in case you need more limitations and more external and internal boundaries such as max-min and greaterthan-smallerThan you can use the PyIntPlus module. PyIntPlus module also has input options for date&time, strings and many more.
Make sure to read and write above mentioned example codes in order to have a better understanding of this problem. Thanks.
Thanks for reading.