I'm trying to sanitize input for a 24-hour clock and the code below is what I came up with. A few lines are repeated and I would like to learn if there's a better pattern to avoid it.
def inputHour():
startHour = input("Starting time (hour): ")
while not startHour.isdigit():
startHour = input("Please enter numbers only (0 - 23): ")
while 0 < int(startHour) > 23:
startHour = input("Invalid hour. Please enter value from 0 to 23: ")
while not startHour.isdigit():
startHour = input("Please enter numbers only (0 - 23): ")
return startHour