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

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