Case8. Two integers are given: D (day) and M (month), which determine the correct date of a non-leap year. Output the values of D and M for the date preceding the specified date.

Solution in Python 3:

import random

M = random.randrange(1,13)
#M=1
month = {
1: 31,
2: 28,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31
}
try:
D_Max = month[M]
D = random.randrange(1,D_Max+1)
#D = 1
print("Date:")
print("Month: {0}. Day: {1}".format(M,D))
if D > 1:
D -= 1
else:
if M == 1:
M = 12
else:
M -=1
D = month[M]
print("Preceding date:")
print("Month: {0}. Day: {1}".format(M,D))
except KeyError as e:
print('Error')