-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatecheck.py
More file actions
124 lines (71 loc) · 3.18 KB
/
datecheck.py
File metadata and controls
124 lines (71 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Tested on Python Version 3.9
# Author: Christian Goeschel Ndjomouo
# Sep 13 2023
# Description: This Python program asks the user to enter a a date in DD / MM /YYYY format,
# and the program checks whether the date is valid or not.
# Request input from user in DD / MM / YYYY format
def input_req():
# User input prompt and split of the date components with "/" as delimiter
user_input = input("Please type in a date in DD / MM / YYYY format: ").split('/')
global split_date
split_date = []
# For loop with an nested try:except statement that tries to iterate through the date_list list and
# stores each <str> list value as an <int> value
if len(user_input) == 3:
try:
split_date.append(int(user_input[0])) # List value in <str> to <int> conversion
split_date.append(int(user_input[1]))
split_date.append(int(user_input[2]))
except:
print("\nFatal Error!\n\nExpected input type: numeric\nActual input type: alphabectic/symbolic\nPlease try again.")
else:
split_date = None
print("Fatal Error! Wrong date format. Expected DD/MM/YYYY")
return split_date
# Date validation function
# Here the date will be checked on its numeric legitimacy
def date_checker(date):
leap_year = False # Leap year boolean variable
# Checking whether year is a leap year or not
# Divisible by 4 without a remainder => Leap year
if date != None:
if date[-1] % 4 == 0:
leap_year = True
# Condition tree if date[-1] is a leap year
if date == None:
print("\n\nDATE IS INVALID!\n\n")
exit
elif leap_year:
if date[1] == 2 and date[0] > 29:
print("Invalid!",date[-1],"is a leap year. Day cannot be", date[0])
exit
elif date[0] >= 32 or date[0] <= 0:
print("Invalid! Day cannot be", date[0])
exit
elif date[1] >= 13 or date[1] <= 0:
print("Invalid! Month cannot be", date[1])
exit
elif date[-1] <= 0:
print("Invalid! Year cannot be", date[-1])
exit
else:
pass
# Condition tree if date[-1] is NOT A LEAP YEAR
else:
if date[1] == 2 and date[0] > 28:
print("Invalid!",date[-1],"is not a leap year. So February only has 28 days. Day cannot be", date[0])
exit
elif date[0] >= 32 or date[0] <= 0:
print("Invalid! Day cannot be", date[0])
exit
elif date[1] >= 13 or date[1] <= 0:
print("Invalid! Month cannot be", date[1])
exit
elif date[-1] <= 0:
print("Invalid! Year cannot be", date[-1])
exit
else:
print("\n\nDATE IS VALID!\n\n")
exit
# Invoke the date_checker function with the return of input_req() as argument
date_checker(input_req())