Previous Day
Programming Basics · Python
TASK
Problem
Given valid `day month` in a non-leap year, print the previous date.
EXAMPLE
Example
1 3
28 2
LIMITS
Constraints
date is valid; non-leap year
LEARN
Theory for this problem
+
`input()` reads data entered by the user. The result of `input()` is initially a string. `split()` separates one input line into individual values using spaces. `map()` applies the specified conversion to each input value. The `-` operator subtracts one number from another. Comparison operators `==`, `!=`, `<`, `>`, `<=`, `>=` compare values and produce `True` or `False`. `or` requires at least one condition to be true. `if` checks a condition and runs its block when the condition is true. `else` runs when the corresponding `if` condition is false. `print()` displays the program result. Print only what the problem statement requires.
**Connection to this task.** An `if` statement chooses a program branch from a Boolean condition. Comparisons produce `True` or `False`, and `elif` lets you test several mutually exclusive cases in order. The expression `day` shows how this idea is applied to the task data.
NEED HELP?
Hints
+
['If `day > 1`, decrement it; otherwise move to previous month and its last day.']
ANSWER
Solution
+
day, month = map(int, input().split())
if day > 1:
day -= 1
else:
if month == 1:
month = 12
else:
month -= 1
if month == 2:
day = 28
elif month == 4 or month == 6 or month == 9 or (month == 11):
day = 30
else:
day = 31
print(day, month)