Check Valid Calendar Date
Validate a day and month for a common year using Python. Students follow nested bounds checks, month groups, and the missing year needed for leap-day support.
1# Take day and month2day = int(input("Enter day: "))3month = int(input("Enter month (1-12): "))45# Check validity6if month < 1 or month > 12:7 print("Invalid month")8elif month in [1, 3, 5, 7, 8, 10, 12]:9 if 1 <= day <= 31:10 print("Valid date")11 else:12 print("Invalid date")13elif month in [4, 6, 9, 11]:14 if 1 <= day <= 30:15 print("Valid date")16 else:17 print("Invalid date")18elif month == 2:19 if 1 <= day <= 28:20 print("Valid date")21 else:22 print("Invalid date")
Output
Enter day: 31 Enter month (1-12): 1 Valid date Enter day: 31 Enter month (1-12): 4 Invalid date Enter day: 29 Enter month (1-12): 2 Invalid date
In this one
- Validate month before day
- Nest bounds checks
- Understand common-year limits
How it works
- 1
Read day and month.
- 2
Reject an invalid month or select its length group.
- 3
Test the day against that group's bounds.
Edge cases
Does April 31 pass?
Does April 31 pass?
No. April allows at most thirty days.
Does February 29 ever pass?
Does February 29 ever pass?
No. This common-year model has no leap-year calculation.
What's going on
The month is validated before the program chooses a day limit. Thirty-one-day months, thirty-day months, and February each have their own nested day check.
Each day comparison includes one as the lower bound. February stops at twenty-eight because the program has no year input. Thus January 31 passes but April 31 and February 29 fail. This is not a full year-aware calendar validator.
Does April 31 pass? No. April allows at most thirty days.
Does February 29 ever pass? No. This common-year model has no leap-year calculation.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why check the month first?
Pick an answer to check it.
Question 2
Does April 31 pass?
Pick an answer to check it.
Questions people ask
Does April 31 pass?
No. April allows at most thirty days.
Does February 29 ever pass?
No. This common-year model has no leap-year calculation.
Why check the month first?
The allowed day count depends on it.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum