When the if condition is false you are not incrementing i
dedent the last line like this.
a = '2011.txt'
years = ['2011', '2011', '2011', '2012', '2013']
i = 0
while i < len(years):
if a.endswith(years[i] + '.txt'):
print(years[i] + '.txt')
i += 1
It's better to just use a for loop though (say goodbye to "off-by-one" bugs)
a = '2011.txt'
years = ['2011', '2011', '2011', '2012', '2013']
for item in years:
if a.endswith(item + '.txt'):
print(item + '.txt')
If you also need a loop counter, use enumerate
a = '2011.txt'
years = ['2011', '2011', '2011', '2012', '2013']
for i, item in enumerate(years):
if a.endswith(item + '.txt'):
print(item + '.txt')