Reputation:
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).
Here is my 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 < 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)
I think this should work, however I keep getting the error IndexError: list index out of range
for the line a = test[n]
What is causing this?
Thank you :)
Upvotes: 0
Views: 1295
Reputation: 8213
The best way to avoid IndexError in this situation is not to use the index. If you want to iterate over every item in the "test" list, just use the for loop:
for a in test:
a = str(a)
...
Simpler, easier to read, and less error prone.
Upvotes: 2
Reputation: 14098
The problem is in this code:
number3 = cow[-1] + 1
number10 = number3 - 1
test = range(1,number10)
n = 0
while n < number3:
a = test[n]
a = str(a)
n = n + 1
The problem is that the test
list has a length equal to number10-2
. You are looping until n == number10
. That means at the end of your while loop, you will be trying to access test[number10]
, but your test
list only has number10-2
elements, so you get an IndexError
.
I have no idea what you're trying to do, so I'm not sure how to fix it, but you need to make sure you don't index test
with a higher index than there are elements in `test.
Upvotes: 1
Reputation: 113988
you are using a bad range
number3 = 10
number10 = 10 -1 = 9
test = range(1,9) # 1,2,3,4,5,6,7,8 #notice how many variables there are
if 9 < number3:
test[9] # error
Upvotes: 4
Reputation: 38217
The IndexError: list index out of range
exception is raised when you are trying to access an element in a list that the list doesn't have; i.e. the list is smaller than you think; for example:
>>> lst = [1, 2, 3]
>>> lst[0] # OK
>>> lst[1] # OK
>>> lst[2] # OK
>>> lst[3] # element 3, i.e. 4th element, doesn't exist in the list
...
IndeError: list index out of range
So basically you need to analyze and potentially debug your algorithm/code and see why the value of n
reaches a value that causes test[n]
to raise IndexError
.
Also, as a side note, the huge block of if
s can be simplified to:
cow = range(10 ** (number1 - 1), 10 ** number1)
Upvotes: 3