144k views
2 votes
Writing a Modular Program in Python

Summary
In this lab, you add the input and output statements to a partially completed Python program. When completed, the user should be able to enter a year, a month, and a day. The program then determines if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31.
Instructions
Notice that variables have been declared for you.
Write input statements to retrieve a year, a month, and a day from the user.
Include the print statements to output the following: month/day/year is a valid date. or month/day/year is an invalid date.
Execute the program entering the following input: month = 5, day = 32 year = 2014
Execute the program entering the following input: month = 9 day = 21 year = 2002
# Program Name: BadDate.py
# Function: This program determines if a date entered by the user is valid.
# Input: Interactive
# Output: Valid date is printed or user is alerted that an invalid date was entered.
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:"))
# Get the month, then the day, then the year
# housekeeping()
# Check to be sure date is valid
if int(year) <= MIN_YEAR: # invalid year
validDate = False
elif int(month) < MIN_MONTH or int(month) > MAX_MONTH: # invalid month
validDate = False
elif int(day) < MIN_DAY or int(day) > MAX_DAY: # invalid day
validDate = False
# Test to see if date is valid and output date and whether it is valid or not
# endOfJob()
if validDate == True:

User Xenph Yan
by
5.1k points

1 Answer

4 votes

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.

Writing a Modular Program in Python Summary In this lab, you add the input and output-example-1
User TheMarko
by
5.6k points