Search code examples
pythonstringindexingindex-error

Index 1 of string of length 2 out of bounds?


I am using Python, attempting Problem 17 of the Euler Project (adding up the number of letters in each number up to 1000).

I have written code to turn each number into a string and systematically work out how many letters each number contains, this code is not attractive, but it is mine. Here is the specific part I am having an issue with:

if len(n)==2:
            print(n,n[1])
            if n[0]=="0":
                n=n[1]
            if n[1]=="0":
                if n[0]=="1":
                    cnt+=3
                    n=""

That print statement is there to show me what the code is trying to read. It works perfectly from 1 to 100, then when it attempts 101 I get an error for line 23 (if n[1]=="0":) saying the string index is out of bounds, but that print function is returning 01 1 which means it is able locate index 1 of this string.

Any ideas?

This is the full code, it's not very attractive, but it's mine.

def numletcount(t):
    n=str(t)
    cnt=0
    while len(n)>0:
        if n == "0":
            n=""
            
        if len(n)==1:
            if n == "1" or n == "2" or n == "6":
                cnt+=3
                n=""
            if n == "4" or n == "5" or n == "9":
                cnt+=4
                n=""
            if n == "3" or n == "7" or n == "8":
                cnt+=5
                n=""
                
        if len(n)==2:
            print(n,n[1])
            if n[0]=="0":
                n=n[1]
            if n[1]=="0":
                if n[0]=="1":
                    cnt+=3
                    n=""
                elif n[0] == "2" or n[0] == "3" or n[0] == "8" or n[0] == "9":
                    cnt+=6
                    n=""
                elif n[0] == "4" or n[0] == "5" or n[0] == "6":
                    cnt+=5
                    n=""
                elif n[-2] == "7":
                    cnt+=7
                    n=""
            else:
                if n[0]=="1":
                    if n[-1] == "1" or n[-1] == "2":
                        cnt+=6
                        n=""
                    elif n[-1] == "5" or n[-1] == "6":
                        cnt+=7
                        n=""
                    elif n[-1] == "3" or n[-1] == "4" or n[-1] == "8" or n[-1] == "9":
                        cnt+=8
                        n=""
                    elif n[-1] == "7":
                        cnt+=9
                        n=""
                elif n[0] == "2" or n[0] == "3" or n[0] == "8" or n[0] == "9":
                    cnt+=6
                    n=n[-1]
                elif n[0] == "4" or n[0] == "5" or n[0] == "6":
                    cnt+=5
                    n=n[-1]
                elif n[0] == "7":
                    cnt+=7
                    n=n[-1]
            
        if len(n)==3:
            if n[1:3] == "00":
                cnt+=7
                n=n[0]
            else:
                if n[0] == "1" or n[0] == "2" or n[0] == "6":
                    cnt+=13
                    n=n[1:3]
                elif n[0] == "4" or n[0] == "5" or n[0] == "9":
                    cnt+=14
                    n=n[1:3]
                elif n[0] == "3" or n[0] == "7" or n[0] == "8":
                    cnt+=15
                    n=n[1:3]
        if len(n)==4:
            cnt+=11
            n=n[1:4]
    print(t,cnt)
    return cnt

Solution

  • you said that its print 01 and 1, so n is 01 and n[0]==0. now you have

    if n[0]=="0":
        n=n[1]
    

    so n is "1" now, and his length is 1