Python Forum
Validating Input (basic check for int etc)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validating Input (basic check for int etc)
#1
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
I am trying to help you, really, even if it doesn't always seem that way
Reply
#2
I've had some feedback elsewhere, and made a few changes to the linked code.

I think I am going to have to do a fairly big rewrite. Some issues around the checking of lists/tuples for int_range. Also, some duplication as I wanted to be able to offer the int_validate() function as callable in its own right as well as being usable from get_input().

Whilst the code basically runs correctly for a user, there are some error situations caused by incorrect calling by the consuming programme that are not caught properly at the moment that I need to address.

I can also simplify some of the type checks.

Any additional feedback from members here on technique and on errors would be greatly appreciated.

These are still early learning days for me.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 421 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [split] formula for validating monetary values? kakos_k9 1 759 Dec-17-2022, 09:28 PM
Last Post: woooee
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,804 Nov-23-2020, 11:01 PM
Last Post: perfringo
  Validating user input WJSwan 2 2,143 Jul-06-2020, 07:21 AM
Last Post: menator01
  Check all input, easy way! Help pls! bntayfur 2 1,810 Jul-05-2020, 10:58 PM
Last Post: bntayfur
  input check help! bntayfur 3 2,368 Jun-02-2020, 11:38 PM
Last Post: bowlofred
  Validating the functionality of the application rpalakodety 1 1,780 Dec-30-2019, 07:58 PM
Last Post: ndc85430
  Validating information from .csv file before executemany mzmingle 7 4,449 Apr-15-2019, 01:40 PM
Last Post: mzmingle
  need help with making a validating function drasil 8 3,758 Mar-28-2019, 10:38 AM
Last Post: perfringo
  how i can check the input type? Firdaos 3 2,867 Dec-13-2018, 11:39 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020