1

I have been working on this program, any kind of help will be appreciated.
program to find the leap years between two years and add them to array...

from array import array

x=int(input("Enter the year "))
print("the year you entered is",x)

while x<=2017:
    if x%4==0:
        print(x)
        n=array('i',[x])
        n.append(x)
        x=x+1
    else:
        x=x+1
print(n)

Output

enter the year 1992
the year you entered is 1992
1992
1996
2000
2004
2008
2012
2016
array('i', [2016, 2016])
5
  • Exactly what is the question? Commented Aug 6, 2017 at 6:17
  • 2
    Why are you trying to use an array.array for this instead of a plain list? Commented Aug 6, 2017 at 6:18
  • Hi Anand, Actually i want the result set to be added into array or list..I just want to know the logic in adding the result set to array/list.Just tried the above program but was not successful in adding the output to array/List Commented Aug 6, 2017 at 6:23
  • 1
    Here's a compact way to do it: list(range(-n//4*-4,2018,4)), but as Vasyl Moskalov mentions this logic doesn't handle century years like 1900 correctly. Commented Aug 6, 2017 at 6:31
  • And here's a version that does the correct Gregorian leap year calculation: [i for i in range(-n//4 * -4, 2020, 4) if i%100 or i%400 == 0] Commented Aug 6, 2017 at 7:00

2 Answers 2

3

The problem is that you're re-setting the value of your array each time a year is divisible by 4. What you want to do is declare your array outside the loop.

from array import array

x=int(input("enter the year from which you want to know the leap year from"))
print("the year you entered is",x)

n=array('i')
while x<=2017:
    if (x % 4 == 0 and x % 100 != 0) or x % 400 == 0:
        print(x)   
        n.append(x)
    x += 1  # we need to add 1 regardless, no need for else  
print(n)
# output: array('i', [1992, 1996, 2000, 2004, 2008, 2012, 2016])
Sign up to request clarification or add additional context in comments.

3 Comments

Also you need to fix leap year test
As for me much simple test is (x%4==0 and x%100!=0) or x%400==0
Thanks a lot @Dor-Ron
2

Move first assignment of n outside the loop and replace while with for. Something like

n=array('i') # or you can use smthg like n=[]
for i in range(i,2018):
    if i%4==0:
        n.append(i)

Besides you have an wrong leap year test. From wiki:

Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 were not leap years, but the years 1600 and 2000 were.Leap year

1 Comment

Good point about the leap year test, maybe the OP doesn't intend to use this code on years <= 1900. But I'm reminded of the Excel Leap Year Bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.