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
- Please original work select a topic related to architectures or infrastructures (Data Lakehouse Architecture). Discussing how you would implement your chosen topic in a data warehouse project Please cite in text references and add weblinksarrow_forwardPlease original work What topic would be related to architectures or infrastructures. How you would implement your chosen topic in a data warehouse project. Please cite in text references and add weblinksarrow_forwardWhat is cloud computing and why do we use it? Give one of your friends with your answer.arrow_forward
- What are triggers and how do you invoke them on demand? Give one reference with your answer.arrow_forwardDiscuss with appropriate examples the types of relationships in a database. Give one reference with your answer.arrow_forwardDetermine the velocity error constant (k,) for the system shown. + R(s)- K G(s) where: K=1.6 A(s+B) G(s) = as²+bs C(s) where: A 14, B =3, a =6. and b =10arrow_forward
- • Solve the problem (pls refer to the inserted image)arrow_forwardWrite .php file that saves car booking and displays feedback. There are 2 buttons, which are <Book it> <Select a date>. <Select a date> button gets an input from the user, start date and an end date. Book it button can be pressed only if the start date and ending date are chosen by the user. If successful, it books cars for specific dates, with bookings saved. Booking should be in the .json file which contains all the bookings, and have the following information: Start Date. End Date. User Email. Car ID. If the car is already booked for the selected period, a failure message should be displayed, along with a button to return to the homepage. In the booking.json file, if the Car ID and start date and end date matches, it fails Use AJAX: Save bookings and display feedback without page refresh, using a custom modal (not alert).arrow_forwardWrite .php file with the html that saves car booking and displays feedback. Booking should be in the .json file which contains all the bookings, and have the following information: Start Date. End Date. User Email. Car ID. There are 2 buttons, which are <Book it> <Select a date> Book it button can be pressed only if the start date and ending date are chosen by the user. If successful, book cars for specific dates, with bookings saved. If the car is already booked for the selected period, a failure message should be displayed, along with a button to return to the homepage. Use AJAX: Save bookings and display feedback without page refresh, using a custom modal (not alert). And then add an additional feature that only free dates are selectable (e.g., calendar view).arrow_forward
- • Solve the problem (pls refer to the inserted image) and create line graph.arrow_forwardwho started the world wide webarrow_forwardQuestion No 1: (Topic: Systems for collaboration and social business The information systems function in business) How does Porter's competitive forces model help companies develop competitive strategies using information systems? • List and describe four competitive strategies enabled by information systems that firms can pursue. • Describe how information systems can support each of these competitive strategies and give examples.arrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage