Aug-04-2018, 01:44 PM
(This post was last modified: Aug-06-2018, 07:42 AM by gruntfutuk.)
For the last few days, I've been playing around with writing a function to validate input to avoid the classic int(input('Number? ')) getting string input problem. It supports input of strings, floats and integers and can also prompt for multiple inputs of the same type and return them as a list. Integers can also be validated against one or more ranges.
The function will keep prompting the user until a valid input is provided. (Whether or not it is useful data in the context of the calling programme is another matter.)
Initially, this was just an exercise to illustrate some ideas for beginners on a FB group. Couldn't help myself but extend it. This is a work in progress, and I might well scrap it and start again.
Suggestions on better techniques and corrections would be welcome. I am sure the code is full of bugs (feel free to point any out to me). I have been remiss in not properly testing it. Think I had better work on that next.
Overview of usage:
The function can replace input and be called as follows:
get_input()
defaults to requiring an integer input. Will not allow no input. No prompt.
get_input('Enter a number: ')
defaults to requiring an integer input. Will not allow no input. Uses supplied argument as prompt string.
get_input('Enter a number: ', type_req = float)
for a floating point number (will also accept an integer)
get_input('Name: ', type_req = str)
get_input('Name: ', type_req = str, blank_ok = True)
gets a string, the second form will allow an empty response.
with blank_ok for numbers a None will be returned but '' for string
get_input('Positive integer: ', int_range = 1)
will only accept number of 1 or higher
get_input('Positive integer: ', int_range = [1, 10])
will only accept aThe number between 1 and 10 inclusive
int_range can have several ranges, with a two element list or tuple for each range:
int_range = [(-100, -10), (20, 50), (1000, 2000)]
would mean number has to be:
-100 <= number <= -10 or 20 <= number <= 50 or 1000 <= number <= 2000
negative_ok is another boolean flag defaulted to True. If set to False, it will not allow a negative integer to be entered (even if specified in the ranges).
These numeric tests are not available for floats and more explicit coding is required in the consuming programme to allow for round errors in floating point numbers.
The input function can also return multiple results.
cont = True means return a list of the required type, enter on its own to terminate data entry
cont_qty = <num> means return a list of <num> elements of the required type
if inc_count = True is also specified then the prompt will be modified with an input number counter. If the prompt message text includes the ± character, this will be used to delimit what prompt text comes before and after the number count (which otherwise defaults to a space after the number).
Runnable code (just the function, so you need to call it, which you can do in the terminal):
https://repl.it/@gruntfutuk/inputvalidationv3
The function will keep prompting the user until a valid input is provided. (Whether or not it is useful data in the context of the calling programme is another matter.)
Initially, this was just an exercise to illustrate some ideas for beginners on a FB group. Couldn't help myself but extend it. This is a work in progress, and I might well scrap it and start again.
Suggestions on better techniques and corrections would be welcome. I am sure the code is full of bugs (feel free to point any out to me). I have been remiss in not properly testing it. Think I had better work on that next.
Overview of usage:
The function can replace input and be called as follows:
get_input()
defaults to requiring an integer input. Will not allow no input. No prompt.
get_input('Enter a number: ')
defaults to requiring an integer input. Will not allow no input. Uses supplied argument as prompt string.
get_input('Enter a number: ', type_req = float)
for a floating point number (will also accept an integer)
get_input('Name: ', type_req = str)
get_input('Name: ', type_req = str, blank_ok = True)
gets a string, the second form will allow an empty response.
with blank_ok for numbers a None will be returned but '' for string
get_input('Positive integer: ', int_range = 1)
will only accept number of 1 or higher
get_input('Positive integer: ', int_range = [1, 10])
will only accept aThe number between 1 and 10 inclusive
int_range can have several ranges, with a two element list or tuple for each range:
int_range = [(-100, -10), (20, 50), (1000, 2000)]
would mean number has to be:
-100 <= number <= -10 or 20 <= number <= 50 or 1000 <= number <= 2000
negative_ok is another boolean flag defaulted to True. If set to False, it will not allow a negative integer to be entered (even if specified in the ranges).
These numeric tests are not available for floats and more explicit coding is required in the consuming programme to allow for round errors in floating point numbers.
The input function can also return multiple results.
cont = True means return a list of the required type, enter on its own to terminate data entry
cont_qty = <num> means return a list of <num> elements of the required type
if inc_count = True is also specified then the prompt will be modified with an input number counter. If the prompt message text includes the ± character, this will be used to delimit what prompt text comes before and after the number count (which otherwise defaults to a space after the number).
Runnable code (just the function, so you need to call it, which you can do in the terminal):
https://repl.it/@gruntfutuk/inputvalidationv3
I am trying to help you, really, even if it doesn't always seem that way