0

I have this problem that I need to work out however it is proving difficult and I do not know where I went wrong. I do not know what I need to put in order to only have 10 years printed per line. My code so far is as follows def main():

leap_start = int(input("Enter a start year: "))
leap_end = int(input("Enter an end year: "))

print("Here is a list of leap years between", leap_start,"and",leap_end, ":" )

for year in range(leap_start, leap_end):

    if (year% 400) !=0 and (year % 1000 == 0) and (year % 4) == 0:
     print ("", end = "")

    elif (year % 4) == 0:
        print (start, end = ",")

    elif (year % 100) == 0 and (year % 400) == 0:
        print (start, end =",")

    for y in range ((len(years)// 10) + 1):
         pri = years [10*y:(y+10) + 10]
         print(*pri, sep = ",")

    else :
      print ("", end = ",")
Z Ahmed
  • 1
  • 4
  • Create a String which you will write 10 years to before printing that entire String – Mark Oct 23 '18 at 20:32

2 Answers2

0

Going off of Mark's suggestion, I propose a different idea:

When printing out each leap year, have a counter that goes from 1 to 10 inside this giant for-loop, and once you hit 10, reset back to 1 and print a new line. Otherwise, separate each item by a comma and space. Hope the idea works as well.

  • I'd rather not, more so to make you think a little about it. I had a very similar problem when I started coding and learned about loops, variable scope, etc. You have a variable (call it `count`, and set it equal to 1) that would be declared outside the loop, and every time you print a leap year, increment it. Once the counter hits 10, print the leap year AND a new line. Otherwise, print the leap year, a comma, and a space. If you absolutely need the code, then I will write it. – Ben Michalowicz Oct 23 '18 at 20:50
0

You solved the input part:

leap_start = int(input("Enter a start year: "))
leap_end = int(input("Enter an end year: "))     # this year is NOT checked 
                                                 # add 1 to if if you want it checked

print("Here is a list of leap years between", leap_start,"and",leap_end, ":" )

You could define a method that returns if a year is a leap year:

def isLeap(y):
    """Leap years are divisible by 4 but not by 100 unless they are divisible by 400"""
    return y % 4 == 0 and not y % 100 == 0 or y % 400 == 0

You filter from the range all years that are leap years (range does not check leap_end itself - add 1 to it if you need to check it as well):

years = [y for y in range(leap_start,leap_end) if isLeap(y)]

You partition the list into sublists (part) that are (at most) 10 items long and print each sublist on one line.

for part in ( years[i:i+10] for i in range(0,len(years),10) ):
    # unpack the list into its 10 elements - print using a seperator of ','
    print(*part, sep=",")

Doku:

Output:

Enter a start year: 1899
Enter an end year: 2001
Here is a list of leap years between 1899 and 2001 :
1904,1908,1912,1916,1920,1924,1928,1932,1936,1940
1944,1948,1952,1956,1960,1964,1968,1972,1976,1980
1984,1988,1992,1996,2000
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69