Sunday 8 September 2013

Getting an error 'string index out of range'

Getting an error 'string index out of range'

I'm new to programming in python and i'm getting this strange error.
Traceback (most recent call last): File "C:/Documents and Settings/All
Users/Documents/python/caeser hacker.py", line 27, in translated =
translated + LETTERS[num] IndexError: string index out of range
Any solutions?
Full code:
caeser cipher hacker
hhtp://inventwithpython.com/hacking {bsd licensed}
message='GUVF VF ZL FRPERG ZRFFNTR.' LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
loop through every possiable key
for key in range (len(LETTERS)):
#it is important to set translated to blank string so that the
#previous iteration's value for translated is cleared.
translated=''
#the rest of the program is the same as the caeser program.
for symbol in message: if symbol in LETTERS: #GET THE ENCRYPTED (OR
DECRYPTED) NUMBER FOR THIS SYMBOL num= LETTERS.find(symbol) # get the
number of the symbol num = num - key
# handle the wrap-around if num is larger that the length of
#LETTERS or less than 0
if num >= 0:
num = num +len(LETTERS)
# add encrypted/decrypted numbers at the end of translad
translated = translated + LETTERS[num]
else:
# just add the symbol without encrypting/decrypting
translated = translated + symbol
print the encrypted/decrypted string to the screen
print(translated)
copy the encrypted /decrypted string to the clipboard

No comments:

Post a Comment