In this article A program to determine if it is a leap year from the Christian era I will write.
A leap year (leap year, intercalary year) is a leap year. On the other hand, a year that is not a leap year is called a common year (English: common year). leap year-Wikipedia
There are three conditions to determine if it is a leap year.
I will write the program under this condition.
** Conditional branching in the function ** Pass n as an argument to a function called “is_leap_year” which means leap year. n is the Christian era.
The result is returned as a leap year for True and a non-leap year for False.
leap_year.py
def is_leap_year(n):
if n % 4 != 0:
return False
if n % 400 == 0:
return True
if n % 100 == 0:
return False
return True
** Get data and display results ** Print the result with print ().
If True “The year XX is a leap year.” If False, "The year XX is not a leap year." Is displayed.
leap_year.py
year = int(input("Please enter the year in the Christian era →"))
if is_leap_year(year):
print(year, "The year is a leap year.", sep="")
else:
print(year, "The year is not a leap year.", sep="")
Overall picture
leap_year.py
def is_leap_year(n):
if n % 4 != 0:
return False
if n % 400 == 0:
return True
if n % 100 == 0:
return False
return True
year = int(input("Please enter the year in the Christian era →"))
if is_leap_year(year):
print(year, "The year is a leap year.", sep="")
else:
print(year, "The year is not a leap year.", sep="")
Execution result
Enter the year in the Christian era → 1859
→ 1859 is not a leap year.
Enter the year in the Christian era → 2000
→ 2000 is a leap year.
This time, Program to determine if it is a leap year I wrote.
I think it's very easy to do Please try it.
Thank you very much.
Recommended Posts