Write a Python function called leap(yr) that determines whether or not a particu
ID: 3619792 • Letter: W
Question
Write a Python function called leap(yr) that determines whether or not aparticular year is a leap year. If the variable ‘yr’ is a leap year, the function will
return True
otherwise, the function should
return False
A year is a leap year if it is divisible by 4. For example, 2004 is a leap year
because 2004/4 is an integer. However, a year isn’t a leap year if it is divisible by
100 (even though it’s also divisible by 4). So, 2100 is not a leap year, even
though 2100 is divisible by 4. [Actually the real rule is a little more complicated,
but we won’t worry about that here.]
You can test to see if x is divisible by y in Python by checking x%y==0. If this is
true, then x/y is an integer.
Here are two sample runs of the program:
>>> leap(2004)
True
>>> leap(2100)
False
Explanation / Answer
-- Small logic was missed in problem description. -- Year that was divided by 400 also leap year def leap(yr): if yr % 100 == 0 and yr % 400 != 0: return 'False'; elif (yr % 4 == 0 and yr % 100 != 0) or (yr % 400 == 0): return 'True'; else: return 'False';
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.