Skip to main content
added 266 characters in body
Source Link
zondo
  • 3.8k
  • 13
  • 27

First of all, a review.

x = 0

I would give a more meaningful name.

year +=1

PEP 8 says:

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() Yours also isn't quite how you want it. You'll need to move the first close parenthesis to after the second one. Right now you are running % on the call to print and (year) instead of calling print() with the result of running % on a string and (year).


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

First of all, a review.

x = 0

I would give a more meaningful name.

year +=1

PEP 8 says:

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

First of all, a review.

x = 0

I would give a more meaningful name.

year +=1

PEP 8 says:

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() Yours also isn't quite how you want it. You'll need to move the first close parenthesis to after the second one. Right now you are running % on the call to print and (year) instead of calling print() with the result of running % on a string and (year).


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
Source Link
zondo
  • 3.8k
  • 13
  • 27

First of all, a review.

x = 0

I would give a more meaningful name.

year +=1

PEP 8 says:

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