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:
try:
val=input(prompt)
return val
except ValueError:
print("error!")
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.