0

How would I use that errorcheck procedure throughout my program so I can check whether or not the user has inputted the correct data type?

def errorcheck():
    valid=False
    while valid==False:
        try:
            ()=True
        except ValueError:
            print("Please enter an appropriate value")
            valid=False

errorcheckage=int(input("How old are you?"))
forename=str(input("What is your firstname?"))
username=forename[0:3]+age
print(username)

1 Answers1

0

You can write a wrapper that implements your loop controls, but it's rather detailed and may not save you much time.

def validator_loop(f: "function to run",
                   *args: "arguments to function",
                   validator=lambda _: True,
                   **kwargs: "kwargs for function"):
    while True:
        try:
            result = f(*args, **kwargs)
        except Exception as e:
            continue  # repeat if it throws any exception
        else:
            if validator(result):
                return result

# note that an entry of "zero" will fail this validation, even though
# 0 is a valid age for some purposes! I'll leave this edge case for you
checkage = validator_loop(input, "How old are you?",
                          validator=lambda s: int(s))
Adam Smith
  • 52,157
  • 12
  • 73
  • 112