Project Euler #19 - Counting Sundays Problem

I have been working on this problem for a while. It's a problem that says you have to find how many Sundays land on the First of the Month in the years 1901 to 2000. I've made it mostly work, but it still outputs 173, instead of 171 (the correct answer).

Does anyone why this is?

months = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}

start, end = 1900, 2001

years = range(start, end)
leap_years = range(1904, 2001, 4)

Sundays = 0
days = 1
for year in years:
  if year in leap_years:
    months[2] = 29 
  else:
    months[2] = 28

  for month in range(1, 13):
    for day in range(1, months[month]+1):
      if days == 7:
        if day == 1:
          print(year, month, day)
          Sundays += 1
        days = 1
      else:
        days += 1


print(Sundays)

Your algorithm is completely correct - just the inputs are wrong. start and end should be:

start, end = 1901, 2000

The range(a, b) function is not inclusive of b, but it is inclusive of a. Thus start should be 1901 instead of 1900.

The end is a bit more tricky, you have to think about the phrasing of the question:

... in the years 1901 to 2000

Think about it - this means every day up to, but not including, Jan 1 2000. Thus the last year that you actually want to iterate through and check is 1999. Thus, setting end to 2000 will cause the range() function to return all the years up to, but not including, 2000. Which is exactly what the question asks for.