Start free

Advanced · Exceptions · 190 of 264

Real dates

Check dates written as YYYY-MM-DD. Use datetime.date.fromisoformat and catch the error for impossible dates such as 2023-02-30.

Input: n, then n dates. Output: valid or invalid for each.

Examples

Input
3
2024-02-29
2023-02-29
2024-13-01
Output
valid
invalid
invalid
Input
1
2000-01-01
Output
valid

One more test checks other inputs when you press Check.

Stuck? The idea behind exceptions

A try block runs code that might fail; its except catches the error and lets the program carry on, instead of stopping.

Catch the error you expect, by name: ValueError for int("abc"), ZeroDivisionError for dividing by 0.

for line in ["4", "x", "7"]:
    try:
        print(int(line) * 2)
    except ValueError:
        print("not a number")

Watch for: A bare except: hides every mistake, your own typing errors too. Name the error.

All the exceptions exercises
Your code
Opening the code editor…
Python 3.14 in your browser
Output
Run shows what your program prints. Check tests it on every example and the hidden tests.

More exceptions exercises