How to Find Leap Year
Table of Contents
Introduction #
Hello in this blog I will show you how to write a program to see when a leap year it will come when it was a leap year and when it is a leap year. :3
here is how you do it the comment in the code also can help you:
Version 1.0 #
import datetime
#print("what years are a leap year?")
year = int(input("Enter a year to see if it is a leap year: "))
# Debug: check the type of variable
#print(type(year))
leap_or_common_year = ''
#print(year)
# if year is not divisible by 4 remainder will be 0
if (year % 4) != 0:
leap_or_common_year = 'not a leap'
elif (year % 100) != 0:
#print("this is a leap year")
leap_or_common_year = 'a leap'
elif not (year % 400) == 0:
leap_or_common_year = 'not a leap'
#print("not a leap year")
else:
#print("not a leap year")
leap_or_common_year = 'not a leap'
# Determine if the given year is in the past, present, future
# Todo: we need to compare it with the present year
# 1. need to know the present year
today = datetime.date.today()
current_year = today.year
#print(f"DEBUG: current year {current_year}")
# 2. compare the given year with the present year
verb_to_be = ''
if year == current_year:
verb_to_be = 'is'
elif year > current_year:
verb_to_be = 'will be'
else:
verb_to_be = 'was'
print(f"The year: {year} {verb_to_be} {leap_or_common_year} year.")
# The year 2024 is a leap year.
# The year 1900 was not a leap year.
# The year 2092 will be a leap year.
check the code yourself and give it random years to see if the number you input is a leap year make sure to go through the code one by one some parts can be difficult the end. :33333ssssss
Do not do this code because there is error in the code that is not working test for yourself.
Improved version 2.0 #
I improved this code because as you can see there is a massive problem when we typed in a random number for example 1900 it would say the year 1900 is not be a leap year so instead I fixed it with this new improved version 2.0 here is the code. :3
import datetime
#print("what years are a leap year?")
given_year = int(input("Enter a year to see if it is a leap year: "))
# Debug: check the type of variable
#print(type(year))
def is_leap_year(year):
# if year is not divisible by 4 remainder will be 0
if (year % 4) != 0:
leap_or_common_year = 'not a leap'
elif (year % 100) != 0:
#print("this is a leap year")
return True
elif not (year % 400) == 0:
return False
else:
#print("not a leap year")
return False
# give value: 'past', 'present', or 'future'
def check_year_tense(year):
today = datetime.date.today()
current_year = today.year
if year == current_year:
return 'present'
elif year > current_year:
return 'future'
else:
return 'past'
affirmative = {'present': 'is', 'past': 'was', 'future': 'will be'}
negative = { 'present': 'is not', 'past': 'was not', 'future': 'will not be' }
# determine if a year is 'past', 'present' or 'future'
check_year = check_year_tense(given_year)
if is_leap_year(given_year):
print(f"The year {given_year} {affirmative[check_year]} a leap year.")
else:
print(f"The year {given_year} {negative[check_year]} a leap year.")
here is how you do it btw when I was working on my code I learnt a new key word called def its a key word where you can make your own function it is a really cool function and thats it byeeeee :3333.