Question

Python Programming Task 2: Leap Years   Part A - Is this a leap year? Write a...

Python Programming

Task 2: Leap Years  

Part A - Is this a leap year?
Write a function is leap year(year) that calculates whether a given (CE) year is a leap year. The function must: • take one argument: a positive integer representing a year (assumed to be CE) • return True if that year was a leap year; return False if not
NOTE: all years that are divisible by four are leap years, unless they are also divisible by 100, in which case they are only leap years if they are also divisible by 400.

Part B - 2000 was a leap year.
Write a function leap year answer(year) that returns the string ‘Year was a leap year’, ‘Year was not a leap year’, ‘Year will be a leap year’, ‘Year will not be a leap year’. For the purpose of tense, you may assume 2020 and any early year are past tense; the year 2021 and onwards are future tense. You may wish to call your function from Part A as part of this function

# Task 2
def is_leap_year(year):
pass


def leap_year_answer(year):
pass

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student,

Thanks for posting an interesting Python programming question to determine whether the argument year is a leap year or not. In the second question to print the message according to the given year and tense.

For the given year to be a leap year any of the below two conditions should be satisfied:-

i) If the year is divisible by 4 and not a century year i.e. not divisible by 100 then it is a leap year

ii) If the year is divisible by 400 i.e. century year then it is a leap year

1. For the given question, I have created and completed both the functions with proper comments. Please find below leapyear.py file contens with proper comments. Please note that both the functions are placed in a single file only and few test input cases are also provided.:-

2. Please find below the complete program in text format for ready copying with proper comments:-

def is_leap_year(year):#defining function is_leap_year

   isLeapYear=False#initializing a boolean variable isLeapYear as False in starting
   #If the year is divisible by 4 and not a century year i.e. not divisible by 100 then it is a leap year
   # or if the year is divisible by 400 i.e. century year then it is a leap year
   if (year%4==0 and year%100!=0) or year%400==0:
       isLeapYear=True;# Setting the isLeapYear as True

   return isLeapYear#Return the isLeapYear
  
def leap_year_answer(year): #defining function leap_year_answer

   if is_leap_year(year): #Checking if the year is leap year by calling is_leap_year function
           if year>2020:#Checking if the year is greater than 2020 then print message for future tense
               print("Year will be a leap year")
           else:#Otherwise print message for past tense
               print("Year was a leap year")
   else:#Otherwise the year is not a leap year
           if year>2020:#Checking if the year is greater than 2020 then print message for future tense
               print("Year will not be a leap year")
           else:#Otherwise print message for past tense
               print("Year was not a leap year")

#Below are few test inputs for the given functions
leap_year_answer(2000)
leap_year_answer(1900)
leap_year_answer(2020)
leap_year_answer(2024)
leap_year_answer(3600)
leap_year_answer(3000)

3. Please find below output screenshot of the above program. Please note that leapyear.py file was placed at D:/.

4. Note:-

i) Please note tha both the functions are placed in a single file leapyear.py present at D:/.

ii) Second function leap_year_answer(year) is using is_leap_year(year) function internally to check whether the argument year is leap year or not.

iii) Important part of the code is marked in bold.

5. Though I tried to explain the concept with comments. Still if you have any query please feel free to reach me through comments section. I will be more than happy to help you further.

6. Please upvote if you like my answer.

Thanks!!

Happy Learning!!

Add a comment
Know the answer?
Add Answer to:
Python Programming Task 2: Leap Years   Part A - Is this a leap year? Write a...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write code in matlab Compute leap years. Write a function (leapyears) that takes two integer arguments...

    Write code in matlab Compute leap years. Write a function (leapyears) that takes two integer arguments (start year and end year) and prints a list of the leap years that occur between them (inclusive of the start and end year). Leap years are divisible by 4. Except that years divisible by 100 are not leap years unless they are also divisible by 400.

  • Complete task using python code From definitions file: month = ‘February’ year = 1200 Problem 2:...

    Complete task using python code From definitions file: month = ‘February’ year = 1200 Problem 2: Leap Years (2 Points) Your task is to write a program that takes in both a month and year (defined in the definitions file) and outputs the number of days for that month. It should take into account whether the year is a leap year or not. The resulting number of days in a given month/year combo is to be assigned to the variable...

  • In this task you are asked to completely implement the following functions (ie. write both the...

    In this task you are asked to completely implement the following functions (ie. write both the function header and function body) 1) You have been tasked to write a Processing function called isleapYear, which receives an integer representing a year. The function should return true if the year is a leap year and false otherwise. (For your information, a year is leap year if it is divisible by 4 but not 100, or is divisible by 400) 2) You have...

  • FINDING THE LEAP YEARS (14 points) From Wikipedia, "A leap year (also known as an intercalary...

    FINDING THE LEAP YEARS (14 points) From Wikipedia, "A leap year (also known as an intercalary year or bissextile year) is a calendar year containing one additional day (or, in the case of lunisolar calendars, a month) added to keep the calendar year synchronized with the astronomical or seasonal year." In the Gregorian calendar, which is in use today, to determine leap year: A year is a leap year if it is divisible by 400 Or it is divisible by...

  • Repeat SPIDER 2 Q1, but write a function that accepts a year as an input argument...

    Repeat SPIDER 2 Q1, but write a function that accepts a year as an input argument and determines whether that year is a leap year. The output should be the variable extra_day, which should be 1 if the year is a leap year and 0 otherwise. Spider two Q1 1. Write code to determine whether a year is a leap year. Use the mod function. The rules for determining leap years in the Gregorian calendar are as follows: 1. All...

  • PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program tha...

    PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program that reads a values for hour and minute from the input test file timeData.txt . The data read for valid hour and valid minute entries. Assume you are working with a 24 hour time format. Your program should use functions called validateHour and validateMinutes. Below is a sample of the format to use for your output file  timeValidation.txt :   ...

  • Use python to code! Q1 - You need to keep track of book titles. Write code...

    Use python to code! Q1 - You need to keep track of book titles. Write code using function(s) that will allow the user to do the following. 1. Add book titles to the list. 2. Delete book titles anywhere in the list by value. 3. Delete a book by position. Note: if you wish to display the booklist as a vertical number booklist , that is ok (that is also a hint) 4. Insert book titles anywhere in the list....

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:             0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...

  • Python programming Part VI: Move Starman by Movement Commands (20 points) Write a function move ()...

    Python programming Part VI: Move Starman by Movement Commands (20 points) Write a function move () that takes the following arguments, in this order I. board: A game board. 2. movement: A string that represents what kind of movement that Starman needs to perform. Four move ment commands are listed below: . 'U' : go up one row ·"D": go down one row : go left one column "R": go right one column Nole: You may assume that the movement...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:      ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT