Shubham Chahal
Shubham Chahal

Reputation: 834

IndexError in python list

Trying to write a python code to encrypt a string.

Encrypts the string and output is an encrypted string.

print "Enter the string "
a=raw_input()
b=len(a)+1
i=0
e=''


while i<b:
  c=''
  if i % 3 == 0:
     c+=a[i]
     e+=chr(ord(c)+5)
     del c
  elif i%3==1:
     c+=a[i]
     e+=chr(ord(c)+2)
     del c
  elif i%3==2:
     c+=a[i]
     e+=chr(ord(c)+6)
     del c     
  i=i+1

print e 

But when on running this script, error comes.

c+=a[i]
IndexError: string index out of range  

Upvotes: 0

Views: 38

Answers (1)

dnit13
dnit13

Reputation: 2496

Problem is when i becomes equal to len(a), then your a[i] will produce IndexError.

There can be a lot of other improvements other than this, like you are always executing c+=a[i] irrespective of the conditions and many more which you should try to figure out yourself.

Upvotes: 1

Related Questions