python – Is it possible to combine 2 functions together to create 1 function? [closed]

You can refactor the code to create one function that handles both cases, by recognizing the parts that are the same, and parameterizing the other parts.

def check_value(prompt):
    while True:
            return val
        except ValueError:

The only difference between the two functions (other than trivial differences like variable names) was the prompt shown by the input function. We make that a parameter to the new unified function, and call it like this:

x = check_input("What's your name?")
y = check_input("What's your age?")

Why you expect input to possibly raise a ValueError is a different question.

Browse More Popular Posts

Leave a Comment