Your code doesn't print the next 20 leap years.
At the time of my posting, your code is as follows:
def loop_year(year):
x = 0
while x < 20:
if year % 4 != 0 and year % 400 != 0:
year +=1
##print("%s is a common year") %(year)
elif year % 100 != 0:
year +=1
print("%s is a leap year") % (year)
x += 1
The necessary conditions for a year to be a leap year are:
- year is divisible by 4
- year is divisible by 400 or not divisible by 100
The line:
if year % 4 != 0 and year % 400 != 0:
is redundant. Any year that is not divisible by 4 is not divisible by 400, since 400 is divisible by 4. All this condition does is skip over years that are not divisible by 4.
The next condition, which is checked only if year % 4 == 0
, checks if year % 100 != 0
.
So we execute the code:
year +=1
print("%s is a leap year") % (year)
x += 1
when
- year is divisible by 4
- year is not divisible by 100
You need to change the condition so that when year is divisible by 400, we execute the next few lines of code. You could change it to:
elif year % 100 != 0 or year % 400 == 0:
Then, you execute:
year +=1
print("%s is a leap year") % (year)
x += 1
At this point, year should be a leap year, but you increment it before printing, so you actually print (leap year + 1) for each leap year. You can fix this incrementing after printing.
print("%s is a leap year") % (year)
year += 1
x += 1
After these changes, your code would look like:
def loop_year(year):
x = 0
while x < 20:
if year % 4 != 0:
year +=1
##print("%s is a common year") %(year)
elif year % 100 != 0 or year % 400 == 0:
print("%s is a leap year") % (year)
year += 1
x += 1
But you still have a problem: when year is divisible by 100 but not by 400, you never increment it so you enter an infinite loop. To fix this, you should increment year outside of your conditionals. Since you increment year regardless of whether or not year is divisible by 4, you can combine your conditional statements, leaving:
def loop_year(year):
x = 0
while x < 20:
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
print("%s is a leap year") % (year)
x += 1
year += 1
Now this works, but it could still be cleaner and faster. Instead of checking all integers above year, you can just check all year divisible by 4. You can simplify even further by using libraries. Here is how I would implement this:
from itertools import count, islice
from calendar import isleap
def leapyears(start):
start += (4 - (start % 4)) % 4
for leap in (year for year in count(start, 4) if isleap(year)):
yield leap
def loop_year(start):
for leap in islice(leapyears(start), 20):
print "{} is a leap year".format(leap)
Here, you make an infinite generator of leap years, then just take the first 20 values and print them.
If you just want to use fewer lines of code and don't care at all about readability, you can use a similar approach:
from itertools import count, islice
def loop_year(s):
for l in islice((y for y in count(s+(4-s%4)%4,4) if y%100!=0 or y%400==0),20):
print "{} is a leap year".format(l)
But definitely don't do that if you're not golfing.