(msg, hint=None, in_type=int, default=None)
return res
Ask for user input of a specific type
Ask for user input of a specific type
Ask for user input of a specific type
def typed_input(msg, hint=None, in_type=int, default=None):
    """Ask for user input of a specific type"""
    res = None
    while (res is None):
        try:
            if (default is not None):
                assert (isinstance(default, in_type))
                input_val = _input(msg + ": [{}] ".format(default)) or default
            else:
                input_val = _input(msg + ": ")
            res = in_type(input_val)
        except ValueError:
            if (hint is None):
                if (in_type == int):
                    print("Please enter an integer number")
                elif (in_type == float):
                    print("Please enter a number")
                elif (in_type == IPv4Address):
                    print("Please enter a valid IPv4 address")
                else:
                    type_str = in_type.__name__
                    print("Please enter a {}".format(type_str))
            else:
                print(hint)
    assert (res is not None)
    assert (isinstance(res, in_type))
    return res
def typed_input ( msg , hint = None , in_type = int , default = None ) : res = None while ( res is None ) : try : if ( default is not None ) : assert ( isinstance ( default , in_type ) ) input_val = _input ( msg + ": [{}] " . format ( default ) ) or default else : input_val = _input ( msg + ": " ) res = in_type ( input_val ) except ValueError : if ( hint is None ) : if ( in_type == int ) : print ( "Please enter an integer number" ) elif ( in_type == float ) : print ( "Please enter a number" ) elif ( in_type == IPv4Address ) : print ( "Please enter a valid IPv4 address" ) else : type_str = in_type . __name__ print ( "Please enter a {}" . format ( type_str ) ) else : print ( hint ) assert ( res is not None ) assert ( isinstance ( res , in_type ) ) return res