0

I keep getting the IndexError: string index out of range, and am wondering what I'm doing wrong with the code.

inFile=open('pa7.cards','r')
cardnum=inFile.readline().strip()

def main():
    while cardnum!='99999':
        Step1(cardnum)
def CheckLen(num):
    if len(num)>=13 and len(num)<=16:
        return True
    else:
        return False
def Step1(num):
    total=0
    for i in range(len(num)-2,0,-2):
        if eval(num[i]*2)>=10:
            i=eval(num[i])*2
            i=str(i)
            i=eval(i[0])+eval(i[1])
            total+=i
        else:
            total+=i
    return total
def Step2(num):
    total=0
    for i in range(len(num)-1,0,-2):
        total+=i
    return total
def Step3(num):
    total=Step1(num)+Step2(num)
    if total%10==0:
        return True
    else:
        return False
##def DetermineType(num):
main()    

This is what the input file looks like:

4388576018402626 
4388576018410707 
37271983 
99999 

This is the error:

Traceback (most recent call last):
  File "C:/Users/Andrew/Desktop/pa7.py", line 47, in <module>
    main()
  File "C:/Users/Andrew/Desktop/pa7.py", line 18, in main
    Step1(cardnum)
  File "C:/Users/Andrew/Desktop/pa7.py", line 30, in Step1
    i=eval(i[0])+eval(i[1])
IndexError: string index out of range
4
  • What about use int() instead of eval()? Commented Nov 11, 2015 at 9:57
  • it gives the same error Commented Nov 11, 2015 at 9:58
  • Yeah...just say that you could use int() instead of eval(). However, never use eval(). About that error, i = int(i[0])+int(i[1]) I think i's length is 1 so you can't do something like i[1]. Commented Nov 11, 2015 at 10:02
  • Agree with Kevin. For i<10 str(i) will generate string length one which then causes the error in i[1] Commented Nov 11, 2015 at 10:03

2 Answers 2

1

You're doing a few Bad ThingsTM there.

The source of your error is that i is built by taking a digit from your "number string", doubling it and converting it back to a string (so "8" becomes "16").

Then you try and access i[1] - which is out of range if i is only 1 character long.

But there are more problems - reusing the name i within the for loop, using eval() (shudder!) where int() would do, iterating over a string using range() instead of slices...

For example,

for i in range(len(num)-2,0,-2):

should be

for digit in num[-2:0:-2]:
Sign up to request clarification or add additional context in comments.

1 Comment

okay so im not to strong in slices yet, but i've tried to make changes to the code, this is what i did, though it just gives me an infinite loop:
0
def Step1(num):
    total=0
    for digit in num[-2:0:-2]:
        digit=int(digit)

        if int(num[digit])*2>=10:
            x=int(num[digit])*2
            x1=str(x)
            x2=int(x1[0])+int(x1[1])
            total+=x2
        else:
            x=int((num[digit]))
            total+=x
    return total

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.