Retry Until Valid
Completed
Keep reading lines until one is a whole number between 1 and 10. For each invalid line print try again. Then print got N.
Input
Several lines; the last is valid.
Output
One line per input.
Example
Input
abc 0 42 7
Output
try again try again try again got 7
Editor
Python
Hints
You can raise ValueError yourself for a valid int that is out of range, so both cases share one except.
Solution explanation
Raising inside the try funnels "wrong format" and "wrong range" into the same handler.
Show solution
while True:
try:
n = int(input())
if not 1 <= n <= 10:
raise ValueError
break
except ValueError:
print("try again")
print(f"got {n}")