Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
3rd Edition
ISBN: 9781590282779
Author: John Zelle
Publisher: Franklin Beedle & Associates
bartleby

Concept explainers

Question
Book Icon
Chapter 7, Problem 18PE
Program Plan Intro

Validation of date input

Program plan:

  • Define the “leap_Year()” function,
    • Check whether the year is not divisible by "4",
      • If the condition is “True”, return “False”.
      • Otherwise, check whether the year is divisible by "100",
        • If it is “True”, check whether the year is divisible by "400",
          • If it is “True”, return “True”.
          • Otherwise, return “False”.
        • Otherwise, return “True”.
  • Define the “main()” function,
    • Execute the “try” statement to raise the exception if it is occurred.
      • Get the input from the user.
      • Split the user input based on "/" using “split()”.
      • Convert the string type inputs into integer type.
      • Check whether either the month is greater than “12” or day is greater than “12”,
        • If it is “True”, print the string “This date is invalid”
        • Otherwise, check whether the day is less than or equal to “28”,
          • If it is “True”, print the string as “This date is valid”.
          • Otherwise, check whether the month is "2" and "29",
            • If it is “True”, check whether the Boolean value return from “leap_Year()” is “False”.
              • If it is “True”, print “This date is invalid”.
              • Otherwise, print “This date is valid”.
            • Otherwise, check whether the day is “31”,
              • If it “True”, check whether the month is either "2" or "4" or "6" or "11",
              • If the above condition is “True”, print invalid.
              • Otherwise, print valid.
            • Otherwise, print “This date is valid”.
          • Catch the exception using “except ValueError” statement, if the exception of type “ValueError” occurs.
        • Handle the exception.
      • Catch the exception using “except” statement, if any type of exception occurs.
    • Call the function “main()”.

Expert Solution & Answer
Check Mark
Program Description Answer

This Python program is to add decisions and exception handling as required making it truly robust.

Explanation of Solution

Program:

File name: “year.py”

#Define the function

def leap_Year(yr):

    #Check whether the year is not divisible by "4"

    if (yr % 4) != 0:

        #Return the boolean value

        return False

    #Otherwise

    else:

        #Check whether the year is divisible by "100"

        if (yr % 100) == 0:

            #Check whether the year is divisible by "400"

            if (yr % 400) ==0:

                #Return boolean value

                return True

            #Otherwise

            else:

                #Return boolean value

                return False

        #Otherwise

        else:

            #Return boolean value

            return True

#Define the function

def main():

    #Make a try

    try:

        #Get the input from the user

        date_Str = input("Enter a date in the form MM/DD/YYYY: ")

        #Split the user input based on "/" using split()

        month_Str, day_Str, year_Str = date_Str.split("/")

        #Convert the string into integer

        mon = int(month_Str)

        #Convert the string into integer

        d = int(day_Str)

        #Convert the string into integer

        yr = int(year_Str)

        #Execute the condition

        if mon > 12 or d > 31:

            #Print the string

            print("This date is invalid.")

        #Otherwise

        else:

            #Check whether the day is less than or equal to 28

            if d <= 28:

                #Print the string

                print("This date is valid.")

            #Check whether the month is "2" and "29"

            elif mon == 2 and d == 29:

                #Check whether the boolean value return from "leap_Year()" is "False"

                if leap_Year(yr) == False:

                    #Print the string

                    print("This date is invalid.")

                #Otherwise

                else:

                    #Print the string

                    print("This date is valid.")

            #Check whether the day is "31"

            elif d == 31:

                '''Check whether the month is either "2" or "4" or "6" or "11"'''

                if mon == 2 or 4 or 6 or 11:

                    #Print the string

                    print("This date is invalid")

                #Otherwise

                else:

                    #Print the string

                    print("This date is valid")

            #Otherwise

            else:

                #Print the string

                print("The date is valid.")

    #Catch the exception

    except ValueError:

        #Handle the exception

        print("Your input was not in the correct form.")

    #Catch the exception

    except:

        #Handle the exception

        print("Something went wrong!")

#Call the function

main()

Sample Output

Output:

Enter a date in the form MM/DD/YYYY: 02/30/2000

The date is valid.

>>>

Additional output:

Enter a date in the form MM/DD/YYYY: 02-05-2000

Your input was not in the correct form.

>>>

Additional output:

Enter a date in the form MM/DD/YYYY: 06/31/2000

This date is invalid

>>>

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Of the five primary components of an information system (hardware, software, data, people, process), which do you think is the most important to the success of a business organization? Part A - Define each primary component of the information system. Part B - Include your perspective on why your selection is most important. Part C - Provide an example from your personal experience to support your answer.
Management Information Systems
Q2/find the transfer function C/R for the system shown in the figure Re ད
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage