title: IndexError: list index out of range in Python using lists
body: <p>I am trying to make something which counts how many numbers in a certain range add up to a number. For example, how many 2 digit numbers contain integers which add up to 17 (89 being one of them).</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import math
  
  print "Hey, lets solve Task 4 :)"
  
  number1 = input ("How many digits do you want to look at? ")
  number2 = input ("What would you like the digits to add up to? ")
  final = []
  
  if number1 == 1:
      cow = range(0,10)
  elif number1 == 2:
      cow = range(10,100)
  elif number1 == 3:
      cow = range(100,1000)
  elif number1 == 4:
      cow = range(1000,10000)
  elif number1 == 5:
      cow = range(10000,100000)
  elif number1 == 6:
      cow = range(100000,1000000)
  elif number1 == 7:
      cow = range(1000000,10000000)
  elif number1 == 8:
      cow = range(10000000,100000000)
  elif number1 == 9:
      cow = range(100000000,1000000000)
  elif number1 == 10:
      cow = range(1000000000,10000000000)
  
  number3 = cow[-1] + 1
  number10 = number3 - 1
  
  
  test = range(1,number10)
  
  n = 0
  while n &lt; number3:
  
      a = test[n]
      a = str(a)
  
      number4 = sum(int(x) for x in a)
  
      if number4 == number2:
          final.append(number4)
  
      n = n + 1
  
  print len(final)
  </code></pre>
  
  <p>I think this should work, however I keep getting the error <code>IndexError: list index out of range</code> for the line a = test[n]</p>
  
  <p>What is causing this?</p>
  
  <p>Thank you :)</p>