Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

IntermediatePhase 1 - Level 2: Nested If & Multiple ConditionsExample 10 of 10
days-in-month.py
1# Take month number
2month = int(input("Enter month number (1-12): "))
3
4# Determine days
5if 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. 1

    Read month as an integer.

  2. 2

    Search the thirty-one-day and thirty-day lists.

  3. 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.