Question

Decisions, decisions, decisions. Computers are terrible at them, so as programmers we need to instruct them...

Decisions, decisions, decisions. Computers are terrible at them, so as programmers we need to instruct them on how to make good ones. In this lab you will write a program that accepts a date in the form “month/day/year” and prints whether or not the date is valid. For example 5/24/1962 is valid, but 9/31/2000 is not. (September has only 30 days.)

Your program, in order to be correct, should be able to pass these simple (but far from complete!) test cases:

2/29/2000 — (valid)

2/29/2001 — (not valid)

4/30/1984 — (valid)

4/31/1984 — (not valid)

0/12/1234 — (not valid)

13/30/2014 — (not valid)

10/24/2014 — (valid)

It is okay with you allowing leading zeros for the month, or day. This is optional, however.

03/04/2017 — (valid)

07/22/2019 — (valid)

00/12/1234 — (not valid)

Generally speaking, I want to see the following structure in your program. This encourages “separation of concerns”:

  1. Obviously, you will be using if-elif-else statements to make decisions in your program.

  2. The function main() should print a friendly message and ask for the input of the date in the required format (see above). It then should parse the date into its day, month and year components. It will pass this information to a “date check” function.

  3. A “date check” function named is_valid_date(day, month, year) that will take these date components of day, month and year and checks to see if that date is valid.

    The function parameters day, month, and year must be integer values. This function is required to return a Python True or False boolean value (reflecting a valid, or invalid date, respectively) that your main() function will use to print out the correct message about the date’s validity.
  4. A function named is_leap_year(year) that calculates leap year needs to be used by your date check function. This formula should be easy to look up.

    The function parameters year must be an integer value. You need to write this function that takes takes a year as a parameter that will then return a Python True or False boolean value (reflecting a leap, or non-leap year, respectively).

Output

Sample output looks like:

This program accepts a date in the form month/day/year and outputs whether or not the date is valid
Please enter a date (mm/dd/yyyy): 2/29/2000
2/29/2000 is valid

or

This program accepts a date in the form month/day/year and outputs whether or not the date is valid
Please enter a date (mm/dd/yyyy): 4/31/1984
4/31/1984 is not valid

Homework Answers

Answer #1

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

def is_leap_year(y):
    if (y % 100 != 0 and y % 400 != 0) or (y % 4 != 0):
        return False
    return True

def is_valid_date(d, m, y):
    if m < 1 or m > 12:
        return False
    if d < 1 or d > 31:
        return False
    if (m == 4 or m == 6 or m == 9 or m == 11) and (d == 31):
        return False
    if m == 2 and d > 28 and (is_leap_year(y)==False):
        return False
    return True


def date_check(l):
    line = l.split("/")
    if len(line) != 3:
        return False
    m = int(line[0])
    d = int(line[1])
    y = int(line[2])
    if(is_valid_date(d,m,y)):
        return True
    return False


def Main():
    print("This program accepts a date in the form month/day/year and outputs whether or not the date is valid")
    date = input("Please enter a date (mm/dd/yyyy):")
    if (date_check(date)):
        print(date + " is valid")
    else:
        print(date + " is not valid")
    


Main()
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python program that reads in the month, day, and year of a date and...
Write a Python program that reads in the month, day, and year of a date and prints it in the dd/mm/yyyy format or mm/dd/yyyy format, the format depending on the user’s preference. The program also prints the message It is a magic date If the product of month and day is equal to the last two digits of the year. For example, April 20 1980 is a magic date because April (numeric value 4) multiplied by 20 is equal to...
Need this program in Java Develop (using C++ or Java language) the software application described below....
Need this program in Java Develop (using C++ or Java language) the software application described below. Write a program that prints the day number of the year, given the date is in the form month-day-year. For example, if the input is 1-1-09, the day number is 1; if the input is 12-25-09, the day number is 359. The program should check for a leap year. A year is a leap year if it is divisible by 4 but not divisible...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java 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. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation....
Using the model proposed by Lafley and Charan, analyze how Apigee was able to drive innovation. case:    W17400 APIGEE: PEOPLE MANAGEMENT PRACTICES AND THE CHALLENGE OF GROWTH Ranjeet Nambudiri, S. Ramnarayan, and Catherine Xavier wrote this case solely to provide material for class discussion. The authors do not intend to illustrate either effective or ineffective handling of a managerial situation. The authors may have disguised certain names and other identifying information to protect confidentiality. This publication may not be...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...