0

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 :)

4
  • I have spent the last 4 hours working on this, this is pretty much the first bit of programming I have done in my entire life (as well as the first time I have used this site). Commented Oct 22, 2013 at 20:59
  • 1
    what sources/books are you using to learn Python? Commented Oct 22, 2013 at 20:59
  • I am just using Google. Do you have any recommendations? Commented Oct 22, 2013 at 21:03
  • learnpythonthehardway.org is quite famous, and free. Commented Oct 22, 2013 at 21:05

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't my loop end as soon as I reached number 9. Shouldn't it realise that 9 isn't less than number 3 and then shut down?
3

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 ifs can be simplified to:

cow = range(10 ** (number1 - 1), 10 ** number1)

2 Comments

you type faster :P (+1)
but your diagnosis is better of course—I didn't delve into the specific cause of the issue :)
2

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.

1 Comment

+1 This is the best way to iterate over every value in a sequence.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.