Home > Mobile >  Pass a keyword variable to a function as an argument
Pass a keyword variable to a function as an argument

Time:10-19

I'm doing a project that is requiring a lot of input validation. I currently have a function defined as follows:

def get_valid_float(inputMessage,errorMessage):
    while True:
        variableInput = input(inputMessage)
        try:
            variableInput = float(variableInput)
            return variableInput
        except ValueError:
            print(errorMessage)

This function allows me to choose a custom message to prompt the user. It will then validate that the user input is indeed a float, and will print a custom error message in the event that it is not. It will loop until the user gives a valid input.

However, I would rather not create a function to validate each and every data type. It seems like it would be best to combine these into one get_valid_input() function, and pass a third argument allowing me to choose what data type I am attempting to verify. For example, get_valid_input(complex,inputMessage,errorMessage).

I am obviously unable to pass a keyword as an argument. This makes me think the only way to do this would to be to do something like this:

def get_valid_float(dataType,inputMessage,errorMessage):
    if dataType == "float"
        while True:
            variableInput = input(inputMessage)
            try:
                variableInput = float(variableInput)
                return variableInput
            except ValueError:
                print(errorMessage)

    elif dataType == "integer"
        while True:
            variableInput = input(inputMessage)
            try:
                variableInput = int(variableInput)
                return variableInput
            except ValueError:
                print(errorMessage)

And so on, with an elif for every data type. Surely there is an easier way to do this, that somehow allows me to execute the line variableInput = {dataType}(variableInput) to confirm that they input a value of data type "dataType". Any ideas?

CodePudding user response:

Just pass as an argument the actual data type, rather than the name of the data type. E.g:

def get_valid_input(dataType, inputMessage, errorMessage):
    while True:
        value = input(inputMessage)
        try:
            value = dataType(value)
            break
        except ValueError:
          print(errorMessage)

You would call it like this:

floatvalue = get_valid_input(float, "enter a float value: ", "that is an invalid float")
intvalue = get_valid_input(int, "enter an integer value: ", "that is an invalid integer")

CodePudding user response:

I am obviously unable to pass a keyword as an argument.

Not sure why you're saying that, but you can! :)

Also no need for error message, just catch all Exceptions (Not recommended but since you are just printing out the error it seems fine here)

The message strings aren't really needed, try using the name of the dataType and the exception's message like this:

def get_valid_data(dataType):
    while True:
        variableInput = input(f"Put in data of type {dataType.__name__}: ")
        try:
            variableInput = dataType(variableInput)
            return variableInput
        except Exception as e:
            print(e)
get_valid_data(int)

>>> Put in data of type int: "hi"
>>> invalid literal for int() with base 10: '"hi"'
  • Related