Hej
Napisałem kawałek kodu, sprawdza czy data jest prawidłowa. Zastanawiam się jak szybko mogę przetestować różne przypadki czy to działa tak jak należy?
#! python3
# Date Detection - Checks if date is correct.
import re
# Regular expression for date in format: DD/MM/YYYY
date_regex = re.compile(r'([0|123]\d)/(\d{2})/(\d{4})')
# Input validator (main loop)
while True:
print('Enter date in format DD/MM/YYYY: ')
date_input = input()
date_entered = date_regex.search(date_input)
if date_entered is not None:
month = date_entered.group(2)
months = ['01', '02', '03',
'04', '05', '06',
'07', '08', '09',
'10', '11', '12']
year = int(date_entered.group(3))
if year > 3000 or year < 1000:
print(f'Error. Enter year in range: 1000-2999 ({year})')
continue
if month not in months:
print(f'Error. Enter month in format MM (range 01-12) ({month})')
continue
# TODO: Shorten the code
day = int(date_entered.group(1))
wrong_day = 'Wrong day.'
if month == '01':
if day not in range(32):
print(wrong_day)
continue
elif month == '02':
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
if day not in range(30):
print(wrong_day)
continue
else:
if day not in range(29):
print(wrong_day)
continue
elif month == '03':
if day not in range(32):
print(wrong_day)
continue
elif month == '04':
if day not in range(31):
print(wrong_day)
continue
elif month == '05':
if day not in range(32):
print(wrong_day)
continue
elif month == '06':
if day not in range(32):
print(wrong_day)
continue
elif month == '07':
if day not in range(31):
print(wrong_day)
continue
elif month == '08':
if day not in range(32):
print(wrong_day)
continue
elif month == '09':
if day not in range(31):
print(wrong_day)
continue
elif month == '10':
if day not in range(32):
print(wrong_day)
continue
elif month == '11':
if day not in range(31):
print(wrong_day)
continue
elif month == '12':
if day not in range(32):
print(wrong_day)
continue
print('Date is valid.')
break
else:
print('Error. Wrong format.')
continue