Concept explainers
Calculate whether a date is valid
Program plan:
- Define the function “isLeapYear()”,
- Check whether the year is not divisible by "4",
- If it 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 function “main()”,
- Get the date from the user.
- Assign month, day, and year by splitting the date by "/" using “split()” method
- Typecast month, day, and year to “int” type.
- Check whether either a month is greater than "12" or day is greater than "31",
- If it is true, prints date as invalid.
- Otherwise, Check whether day is less than or equal to "28",
- If the condition is “True”, print as valid.
- Otherwise, check whether month is "2" and day is "29",
- If it is “True” Check whether the return value from “isLeapYear()” is “False”,
- If “True” print invalid.
- Otherwise, print valid.
- Otherwise, check whether day is equal to “31”,
- If “True”, Check whether month is either "2" or "4" or "6" or "11",
- If “True” print invalid.
- Otherwise, print valid.
- Otherwise, print valid.
- If “True”, Check whether month is either "2" or "4" or "6" or "11",
- Call the function “main()”.
This Python program accepts a date in the form month / day / year and prints whether the date is valid or not.
Explanation of Solution
Program:
File name: “Leap.py”
#Define the function isLeapYear()
def isLeapYear(y):
#Check whether the year is not divisible by "4"
if (y % 4) != 0:
#Return false
return False
#Otherwise
else:
#Check whether the year is divisible by "100"
if (y % 100) == 0:
#Check whether the year is divisible by "400"
if (y % 400) ==0:
#Return true
return True
#Otherwise
else:
#Return false
return False
#Otherwise
else:
#Return true
return True
#Define the function main()
def main():
#Get the date from the user
date=eval(input("Enter date"))
'''Assign month, day, and year by splitting the date by "/" using split() method'''
month_Str, day_Str, year_Str = date.split("/")
#Typecast month to int type
mon = int(month_Str)
#Typecast day to int type
d = int(day_Str)
#Typecast year to int type
yr = int(year_Str)
'''Check whether either a month is greater than "12" or day is greater than "31"'''
if mon > 12 or d > 31:
#Print a message
print("This date is invalid.")
#Otherwise
else:
#Check whether day is less than or equal to "28"
if d <= 28:
#Print a message
print("This date is valid.")
#check whether month is "2" and day is "29"
elif mon == 2 and d == 29:
#Check whether the return value is false
if isLeapYear(yr) == False:
#Print a message
print("This date is invalid.")
#Otherwise
else:
#Print a message
print("This date is valid.")
#Check whether day is "31"
elif d == 31:
'''Check whether month is either "2" or "4" or "6" or "11"'''
if mon == 2 or 4 or 6 or 11:
#Print date is invalid
print("This date is invalid")
#Otherwise
else:
#Print date is valid
print("This date is valid")
#Otherwise
else:
#Print dte is valid
print("The date is valid.")
#Call the function main()
main()
Output:
Enter date'6/29/1200'
The date is valid.
>>>
Additional Output:
Enter date'12/32/1500'
This date is invalid.
>>>
Additional Output:
Enter date'13/28/1999'
This date is invalid.
>>>
Want to see more full solutions like this?
Chapter 7 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- Discuss 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_forward
- Write .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_forward
- who 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_forwardData communıcatıon digital data is transmitted via analog ASK and PSK are used together to increase the number of bits transmitted a)For m=8,suggest a solution and define signal elements , and then draw signals for the following sent data data = 0 1 0 1 1 0 0 0 1 0 1 1arrow_forward
- DatacommunicationData = 1 1 0 0 1 0 0 1 0 1 1 1 1 0 0a) how many bıts can be detected and corrected by this coding why prove?b)what wıll be the decision of the reciever if it recieve the following codewords why?arrow_forwardpattern recognitionPCA algor'thmarrow_forwardConsider the following program: LOAD AC, IMMEDIATE(30) ADD AC, REGISTER(R1) STORE AC, MEMORY(20) Given that the value of R1 is 50, determine the value stored at memory address 20 after the program is executed. Provide an explanation to support your answer.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning