Answer:
Add these statements to the given code:
if validDate == True: #if the date is valid
print(str(month)+'/'+str(day)+'/'+str(year) + " is a valid date") #prints the statement in month/day/year format. str() converts the values of month day and year to string
else: #if data is not valid
print(str(month)+'/'+str(day)+'/'+str(year) + " is an invalid date") #prints this statement in month/day/year format
Step-by-step explanation:
Here is the complete program:
validDate = True
MIN_YEAR = 0
MIN_MONTH = 1
MAX_MONTH = 12
MIN_DAY = 1
MAX_DAY = 31
year = int(input("Enter year:"))
month = int(input("Enter month:"))
day = int(input("Enter day:"))
if int(year) <= MIN_YEAR:
validDate = False
elif int(month) < MIN_MONTH or int(month) > MAX_MONTH:
validDate = False
elif int(day) < MIN_DAY or int(day) > MAX_DAY:
validDate = False
if validDate == True:
print(str(month)+'/'+str(day)+'/'+str(year) + " is a valid date")
else:
print(str(month)+'/'+str(day)+'/'+str(year) + " is an invalid date")
I will explain the program with an example:
Suppose user enters 2002 as year, 9 as month and 21 as day so
year = 2002
month = 9
day = 21
All these are integers
validDate is set to True initially.
MIN_YEAR is set to 0 initially.
MIN_MONTH is set to 1 initially
MAX_MONTH is set to 12 initially
MIN_DAY is set to 1 initially
MAX_DAY is set to 31 initially
The first if condition if int(year) <= MIN_YEAR: checks if the year is less than MIN_YEAR. As the value of year is 2002 and that of MIN_YEAR is 0 so year is not less than or equals to MIN_YEAR. So this if condition evaluates to false. Thus the program moves to the elif part. The value of validDate remains true.
The elif condition elif int(month) < MIN_MONTH or int(month) > MAX_MONTH: checks if the month is less than MIN_MONTH or greater than MAX_MONTH . Since the value of month is 9 so it is not less than value of MIN_MONTH which is 1 and it is not greater than the value of MAX_MONTH which is 12. So this condition evaluates to false. Thus the program moves to the second elif part. The value of validDate remains true.
The elif condition elif int(day) < MIN_DAY or int(day) > MAX_DAY: checks if the day is less than MIN_DAY or greater than MAX_DAY. This is also not true because of value of day is 21 and it is not less than value of MIN_DAY which is 1 and it is not greater than value of MAX_DAY which is 31. So this condition also evaluates to false. So the program moves to the next statement. However the value of validDate remains true.
The next statement: if validDate == True: evaluates to true because none of the above if elif conditions evaluate to true. Since this condition evaluate to true then statement in this if part executes which is:
print(str(month)+'/'+str(day)+'/'+str(year) + " is a valid date")
This is a print statement that prints the output on screen. str() method is used to convert the values of month, day and year to string form. So the output of this entire program is:
9/21/2002 is a valid date
The program along with its output is attached in a screenshot.