Days in Month
Take a month number (1-12) and print the number of days in that month (ignore leap years).
IntermediatePhase 1 - Level 2: Nested If & Multiple ConditionsExample 10 of 10
days-in-month.py
1# Take month number2month = int(input("Enter month number (1-12): "))34# Determine days5if month in [1, 3, 5, 7, 8, 10, 12]:6 print("31 days")7elif month in [4, 6, 9, 11]:8 print("30 days")9elif month == 2:10 print("28 days")11else:12 print("Invalid month")
Output
Enter month number (1-12): 2 28 days Enter month number (1-12): 4 30 days Enter month number (1-12): 1 31 days
What's going on
Group months by number of days.
Key Concepts:
31 days: Jan, Mar, May, Jul, Aug, Oct, Dec
30 days: Apr, Jun, Sep, Nov
28 days: Feb (ignoring leap year)
Use 'in' operator for list membership
🔗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