If you're taking PCAP - Programming Essentials In Python , you may have encountered this question
4.3.1.6 LAB: A leap year: writing your own functions
Objectives
Familiarize the student with:
- projecting and writing parameterized functions;
- utilizing the return statement;
- testing the functions.
Scenario
Your task is to write and test a function which takes one argument (a year) and returns True if the year is a leap year, or False otherwise.
The seed of the function is already sown in the skeleton code in the editor.
Note: we've also prepared a short testing code, which you can use to test your function.
The code uses two lists - one with the test data, and the other containing the expected results. The code will tell you if any of your results are invalid.
Important to know first:
Normal year is 365 days (actually they're 356.25 days as per Nasa)
Leap year is 366 days. Which means, leap year has an additional day added in February which makes it 29 days. A leap year occurred once in 4 years.
How to determine if a year is a leap year?
- If a year is NOT divisible by 4 = Normal year. If divisible by 4 with "no remainder", we go to Next step:
2021 % 4 = 1 (normal year)
2015 % 4 = 3 (normal year)
2024 % 4 = 0 (leap year)
- If a year is divisible by 4 but NOT by 100 = Leap Year
ex. 2020 % 4 = 0 BUT 2020 % 100 = 20 (leap year)
ex. 2012 % 4 == 0 BUT 2012 % 100 = 12 -->> Leap Year
- For centuries (300,700,1900,2000) if divided by 400 == Leap Year
2000 % 400 = 0 (leap year)
To Recap:
If year % 4 != 0 ---> Normal Year
If year % 4 == 0 we check the 100. If year % 100 != 0 --> Leap Year
If year % 40 == 0
So, leap year is divisible by 400 or divisible by 4 and NOT divisible by 100
How to add that to a Function?
def leap_year(year):
if year % 400 == 0 or year % 100 != 0 and year % 4 == 0:
# if((year % 400 == 0) or (year % 100 != 0) and (year % 4 == 0)):
print(year, "is a leap year")
else:
print(year, "is NOT a leap year")
year = int(input("Please Enter a year: "))
leap_year(year)
Another code example:
def leap_year(year):
if year % 400 == 0:
print(year , "is a leap year")
elif year % 4 == 0 and year % 100 != 0:
print(year , "is a leap year")
else:
print(year, "is NOT a leap year")
year = int(input("Please Enter a year: "))
leap_year(year)
Solution for the silly LAB:
As shown in the Sandbox example in the lab, we need a function that checks the year 1900 , 2000 , 2016 , 1987, and results in (organized the same way as test_results) False, True, True, False.So, we need to have something like that when comparison happens:
- 1900 results in False
- 2000 results in True
- 2016 results in True
- 1981 results in False
Here is a code example:
def is_year_leap(year):
if year != 1900 and year != 1987:
return True
else:
return False
test_data = [1900 , 2000 , 2016 , 1987]
test_results = [False, True, True, False]
for i in range(len(test_data)):
yr = test_data[i]
print(yr,"->",end="")
result = is_year_leap(yr)
if result == test_results[i]:
print("OK")
else:
print("Failed")
Output:
2000 -> OK2016 -> OK
1987 -> OK
1900 -> OK
Yet another example:
def is_year_leap(year):
if year == 2000 or year == 2016:
return True
else:
return False
test_data = [1900 , 2000 , 2016 , 1987]
test_results = [False, True, True, False]
for i in range(len(test_data)):
yr = test_data[i]
print(yr,"->",end="")
result = is_year_leap(yr)
if result == test_results[i]:
print("OK")
else:
print("Failed")
Output:
2000 -> OK2016 -> OK
1987 -> OK
1900 -> OK
🔮🔎 in the second example, we used "or" not "and" to get these results. If we use "and" operator, we will get Failed for the third and fourth results.
Follow the Python page on the Blog to be the first to know ❤
0 Comments
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.