Validate ages

Write parse_age(text) that returns the age as an int, and raises ValueError when the text is not a whole number or the age is outside 0 to 150. Print OK AGE for valid lines and Invalid for the rest.

What to build

A function that raises an error for any bad value.

Requirements

  • Write parse_age(text), raising ValueError for anything that is not a whole number from 0 to 150.
  • Print OK AGE or Invalid for each line.

Input

The first line has n (1 to 100). Each of the next n lines is some text.

Output

One line per input line.

Constraints

n is from 1 to 100.

Example

Input:

3
20
-1
abc

Output:

OK 20
Invalid
Invalid

Why it works

int already raises ValueError for bad text, and raising the same error for out-of-range ages means the caller handles both with one except.

Examples

Example 1 · Input
3
20
-1
abc
Expected output
OK 20
Invalid
Invalid
Example 2 · Input
1
150
Expected output
OK 150

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

int(text) already raises ValueError for text that is not a number.

Hint 2

Raise ValueError yourself for numbers outside the range.

Solution

Show one correct solution (try it yourself first)
def parse_age(text):
    age = int(text)
    if age < 0 or age > 150:
        raise ValueError("age out of range")
    return age

n = int(input())
for _ in range(n):
    line = input()
    try:
        print("OK", parse_age(line))
    except ValueError:
        print("Invalid")

Lesson: Raising your own errors →