Knowledge organisersDefensive Design
Reusable validation routines.
Writing validation as a function makes it reusable. A validation function takes input, checks it, and either returns the valid value or keeps asking until it's correct.
def get_int_in_range(prompt, low, high):
value = int(input(prompt))
while value < low or value > high:
print("Must be " + str(low) + "-" + str(high))
value = int(input(prompt))
return value
age = get_int_in_range("Age: ", 0, 120)