I agree with ichabod801 that try..except is way to go. It can be wrapped into function to separate what from how.
If it's about validating user input then one possibility is (without 'quit' part, it's unclear what function should return in that case):
If it's about validating user input then one possibility is (without 'quit' part, it's unclear what function should return in that case):
>>> def validate_integer(request): ... while True: ... answer = input(request) ... try: ... return int(answer) ... except ValueError: ... print(f'Expected integer but got {answer}') ... >>> validate_integer('Please enter integer: ') Please enter integer: a Expected integer but got a Please enter integer: -1 -1
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.