Days in Month

Take a month number (1-12) and print the number of days in that month (ignore leap years).

Logic BuildingIntermediate
Logic Building
# Take month number
month = int(input("Enter month number (1-12): "))

# Determine days
if month in [1, 3, 5, 7, 8, 10, 12]:
    print("31 days")
elif month in [4, 6, 9, 11]:
    print("30 days")
elif month == 2:
    print("28 days")
else:
    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

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