0

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  
1
  • Here's a hint: give your identifiers meaningful names. Even with this short program, it's much more difficult than it should be to follow what is happening to what, and what each thing represents. Please read the Python Style Guide for more tips on improving your coding style. Commented May 12, 2016 at 17:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

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.