Spike Lee
Spike Lee

Reputation: 13

How to determine weather a year is a leap year, and the years thereafter in Python?

I have to write a for loop program that asks the user to input a year and a number of years, to determine firstly if the year entered is a leap year, the secondly enter the number of years thereafter to check if those are leap years as well.

My output should look like this:

What year do you want to start with? 1994
How many years do you want to check? 8
1994 isn’t a leap year
1995 isn’t a leap year
1996 is a leap year
1997 isn’t a leap year
1998 isn’t a leap year
1999 isn’t a leap year
2000 is a leap year
2001 isn’t a leap year

The code I have thus far works to check the specific year, but I am struggling to loop it so that it continues for the years thereafter.

Here is my code:

print ("""Enter a year to see if it is a leap year,
then enter the number of years there after you want to check""")

year_start = int(input("\nWhat year do you want to start with?\n"))
num_of_years = int(input("How many years do you want to check?\n"))

# I can't figure out the 'for loop' syntax to loop this statement
if (year_start % 4) == 0:  
   if (year_start % 100) == 0:  
       if (year_start % 400) == 0:  
           print (f"{year_start} is a leap year")  
       else:  
           print (f"{year_start} isn't a leap year")  
   else:  
       print (f"{year_start} is a leap year")  
else:  
   print (f"{year_start} isn't a leap year")  

Upvotes: 0

Views: 377

Answers (3)

ErdoganOnal
ErdoganOnal

Reputation: 880

Only division by 4 is not enough for finding leap year. You have to check 100 and 400.

Please check https://en.wikipedia.org/wiki/Leap_year#/media/File:Leap_Centuries.jpg for leap year.

def is_leap_year(year: int) -> bool:
    # Returns True if the given year is a leap year
    return bool((not year % 400) or ((not year % 4) and year % 100))

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71580

Try changing your code to:

print ("""Enter a year to see if it is a leap year,
then enter the number of years there after you want to check""")

year_start = int(input("\nWhat year do you want to start with?\n"))
num_of_years = int(input("How many years do you want to check?\n"))

# I can't figure out the 'for loop' syntax to loop this statement
for i in range(year_start, year_start + num_of_years):
    if (i % 4) == 0:  
        print (f"{i} is a leap year")  
    else:  
       print (f"{i} isn't a leap year")

Example output:

Enter a year to see if it is a leap year,
then enter the number of years there after you want to check

What year do you want to start with?
1994
How many years do you want to check?
8
1994 isn't a leap year
1995 isn't a leap year
1996 is a leap year
1997 isn't a leap year
1998 isn't a leap year
1999 isn't a leap year
2000 is a leap year
2001 isn't a leap year

You can use a for loop and use range for the sequence to iterate. Then you only need (i % 4) == 0, and you don't need the other conditions.

Upvotes: 1

Nour
Nour

Reputation: 210

Use range(num_of_years) as follows:

for i in range(num_of_years):

you should encapsulate all the leap year logic inside that for loop and make some adjustments but you should be able to figure that on your own, good luck.

Upvotes: -1

Related Questions