On 09/22/2016 06:40 AM, Sayth Renshaw wrote:
[snip...]


True it failed, just actually happy to get it to fail or pass successfully on 
int input.

Just felt it was a clearer and more consistent approach to verifying input, 
then most of the varied and rather inconsistent approaches I have seen in 
trying to get this to work.

Half opt for try except the other half if else and then implement them largely 
differently. Every many and varied approach str2bool(), isalpha() using list 
with isinstance(var, [ int, str, bool]) etc.

Anyway back to the old drawing board.

Cheers

Sayth


IMHO... This sort of thing deserves to be written as a separate function which can then be called anytime, anyplace. This keeps your validation and repeated input in one place and is done automatically, and lets your main program simply assume the input is valid.

For example, here is the help message for the number input function I wrote for my personal utility library:

---------------
get_num(prmt, min_val=None, max_val=None, flt=False)

Get a number from the console

    Parameters:
        prmt:       The prompt to be used with the input function, required.
        min_val:    The minimum permissible value, or None if no minimum (the 
default)
        max_val:    The maximum permissible value, or None if no maximum (the 
default)
        flt:        If True, accepts and returns floats
                    If False, accepts and returns integers (the default)

    Invalid input or out-of-range values are not accepted and a warning message
        is displayed.  It will then repeat the input prompt for a new value.
---------------

Admittedly, this may be overkill for general use, but it IS very handy. Even if it is overkill, it is still very easy to use. Similar validated input functions are useful as well — such as a Yes/No function among others.

If you're interested, I could post the source for this version, but if you think of it as a generic function rather than for a specific situation, it is pretty straight-forward to write. something similar.

--
     -=- Larry -=-
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to