Days in Month
Find a month's usual day count with Python membership tests. Beginners group month numbers, handle invalid choices, and see why February needs a year to vary.
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
In this one
- Group values with membership
- Separate February
- Validate month numbers
How it works
- 1
Read month as an integer.
- 2
Search the thirty-one-day and thirty-day lists.
- 3
Handle February separately, otherwise print Invalid month.
Edge cases
What does month 13 produce?
What does month 13 produce?
Invalid month, because it matches none of the allowed choices.
Does February ever print 29?
Does February ever print 29?
No. This version does not ask for a year or apply leap-year rules.
What's going on
Months with the same length can share one branch. The first list contains the seven thirty-one-day months, while the next list contains the four thirty-day months.
February has its own branch and always prints twenty-eight here. With month 2, that is the selected message. Because no year is read, this is a common-year lookup rather than a complete leap-year calendar calculation.
What does month 13 produce? Invalid month, because it matches none of the allowed choices.
Does February ever print 29? No. This version does not ask for a year or apply leap-year rules.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does month in [...] test?
Pick an answer to check it.
Question 2
What does month 13 produce?
Pick an answer to check it.
Questions people ask
What does month 13 produce?
Invalid month, because it matches none of the allowed choices.
Does February ever print 29?
No. This version does not ask for a year or apply leap-year rules.
What does month in [...] test?
Membership in the listed month numbers.
๐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