Check Valid Calendar Date
Take day and month and check if it forms a valid calendar date (ignoring leap years).
AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 3 of 10
check-valid-calendar-date.py
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
What's going on
Check day validity based on month.
Key Concepts:
31 days: Jan, Mar, May, Jul, Aug, Oct, Dec
30 days: Apr, Jun, Sep, Nov
28 days: Feb (ignoring leap year)
Validate day range for each month type
🔗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