First of all, a review.
x = 0
I would give a more meaningful name.
year +=1
Always surround these binary operators with a single space on either side: assignment (
=
), augmented assignment (+=
,-=
etc), comparisons (==
,<
,>
,!=
,<>
,<=
,>=
,in
,not in
,is
,is not
), Booleans (and
,or
,not
).
(Emphasis mine)
if ...:
...
year += 1
else:
...
year += 1
If you are doing the same thing regardless of the if
statement, move that line after the if
and else
.
print("%s is a leap year") % (year)
%
format strings are not officially deprecated, but it is recommended to use .format()
Now for the better ways.
There's a fancy little function that was made for checking leap years: calendar.isleap
from calendar import isleap
def loop_year(year):
found = 0
while found < 20:
if isleap(year):
print("{} is a leap year").format(year)
found += 1
else:
print("{} is a common year").format(year)
year += 1
If you don't care about the common years (based on your commenting out that line), remove the else
:
from calendar import isleap
def loop_year(year):
found = 0
while found < 20:
if isleap(year):
print("{} is a leap year").format(year)
found += 1
year += 1