Dec-05-2020, 03:31 PM
Getting input from a user is something you will use a lot. This is a modified version of a function I wrote so I don't have to keep re-writing user input code over and over. It has been modified to let you specify a list of allowable values.
This line gets the input and converts it to a number. Instead of using int() to do the conversion I let the caller specify a converter function and provide int() as the default converter.
This is some code I wrote to test the function.
def input_number(prompt='', values=None, conv=int): """Input a number. values : An optional list of allowable values conv : The value converter, int() is the default value returns the input number """ while True: try: result = conv(input(prompt)) if values and not result in values: raise ValueError return result except ValueError: print("Input is invalid")This is the start of a function named "input_number". It takes multiple arguments. Default values are provided for all the arguments making them all optional.
def input_number(prompt='', values=None, conv=int)This is a docstring. It is a description of the function.
"""Input a number. values : An optional list of allowable values conv : The value converter, int() is the default value returns the input number """Docstrings aren't required, but I find them useful. If I am using this function later and don't remember the arguments I can just type "help(input_number)" at the Python prompt.
Output:>>> help(input_number)
Help on function input_number in module __main__:
input_number(prompt='', values=None, conv=<class 'int'>)
Input a number.
values : An optional list of allowable values
conv : The value converter, int() is the default value
returns the input number
I know users will make mistakes typing input, so I give them multiple chances to enter a value and catch when they make an error.while True: try: ... except ValueError: ...try and except will "catch" errors that occur in the code between the try and except statements. This exception handler catches errors of type "ValueError". Python will raise a ValueError if you call int() and pass it a string that does not represent an integer. For example typing 'a' or '3.14'. The line(s) after "except" is executed when an exception occurs.
This line gets the input and converts it to a number. Instead of using int() to do the conversion I let the caller specify a converter function and provide int() as the default converter.
result = conv(input(prompt))This code verifies the input value against a list of allowable values. The caller may want to allow all numbers, so this test is only performed if "values" is specified in the function call.
if values and not result in values: raise ValueErrorIf the result is not in the values list the code raises a ValueError. Our exception handler will catch this error.
This is some code I wrote to test the function.
odd_values = [1, 3, 5, 7, 9] print(input_number(f'Enter number {odd_values}: ', odd_values))
Output:Enter number [1, 3, 5, 7, 9]: 2
Input is invalid
Enter number [1, 3, 5, 7, 9]: a
Input is invalid
Enter number [1, 3, 5, 7, 9]: 3
3
I think this function, or one like it, could be useful for code that takes user input but has special input limitations.