Concept explainers
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”.
- If it is “True”, check whether the year is divisible by "400",
- Check whether the year is not divisible by "4",
- 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”.
- If it is “True”, check whether the Boolean value return from “leap_Year()” is “False”.
- 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()”.
- Execute the “try” statement to raise the exception if it is occurred.
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()
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?
Chapter 7 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- Give several reasons why exception-handling techniques should not be used for conventional program control.arrow_forwardWhat is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.arrow_forwardWhat is the importance of exception handling? Explain the use of try, catch and finally in exception handling with the help of example.arrow_forward
- i need the answer quicklyarrow_forwardIf a programming language does not provide exception-handling facilities, it is common to have subprograms indicate an error either through return value or through an error out parameter. What are some disadvantages of this approach comapared to exception-handling facilities provided by programming languages such as C++ and Java?arrow_forward1. Write a program to demonstrate exception propagation in java?arrow_forward
- The following code executes successfully. What is wrong with the implementationfrom a design perspective?def CalculateYearlyInterest(balance, interestRate): try: return balance * interestRate except Exception as ex: print(ex) except ArithmeticError as ex: print(ex)CalculateYearlyInterest(50000, 1.10)arrow_forwardC++arrow_forwardOverloading an operator or function means what? Its benefits? How might programmatic exception handling prevent user errors?arrow_forward
- What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling.arrow_forwardIs it compulsory for a Try Block to be followed by a Catch Block in Java for Exception handling?arrow_forwardWhen someone "overloads" a function or an operator, what precisely does this imply? What are some of the benefits associated with it? How can the user be protected from making mistakes by using the many different exception handling mechanisms that might be programmed?arrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage