script_19.py - Month Days Calculator
Code
#!/usr/bin/env python3
"""Month days calculator"""
month = int(input("Enter month number (1-12): "))
if month in [1, 3, 5, 7, 8, 10, 12]:
print("This month has 31 days")
elif month in [4, 6, 9, 11]:
print("This month has 30 days")
elif month == 2:
print("This month has 28 or 29 days")
else:
print("Invalid month number")
Explanation
Line 4: month = int(input("Enter month number (1-12): "))
- int() converts to integer type
- month stores integer (1-12 expected)
Line 6: if month in [1, 3, 5, 7, 8, 10, 12]:
- [] creates a list literal containing integers
- in checks membership in the list
- Returns True if month matches any number in the list
- Months with 31 days: Jan, Mar, May, Jul, Aug, Oct, Dec
- More readable than if month == 1 or month == 3 or...
Line 8: elif month in [4, 6, 9, 11]:
- Another list literal with 30-day months
- April, June, September, November
- Same in membership check
Line 10: elif month == 2:
- February is special case (leap years)
- Single value check with == operator
- No need for list with one element
Line 12: else:
- Catches invalid input (month < 1 or month > 12)
- Fallback for numbers outside valid range
- Example: 0, 13, 100, -5 all trigger this