2 Replies - 326 Views - Last Post: 03 February 2014 - 12:19 AM Rate Topic: -----

#1 dakhipp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 06-December 13

help with an encrypt/decrypt script

Posted 02 February 2014 - 10:31 PM


def decrypt(encryptedString):
    for key in range(1,101):
        print("Key:", key, "->Decoded Message: ")
        for encryptChar in range(len(encrpytedString)):
            curLetter = encryptedString[encryptChar]
            toNum = ord(curLetter)
            if(toNum - key < 32):
                decryptChar = ((toNum - key) + 127) - 32
            else:
                decryptChar = toNum - key
            print(chr(decryptChar))

def encrypt(regString, key):
    for regChar in range(len(regString)):
        curLetter = regString[regChar]
        toNum = ord(curLetter)
        if(toNum + key > 126):
            encryptChar = ((toNum + key) - 127) + 32
        else:
            encryptChar = toNum + key
        print(chr(encryptChar)) 



regMessage = input("Enter a regular message to encode:")
mesKey = int(input("Enter a key value (between 0 and 100) for encoding:"))
encMessage = encrypt(regMessage, mesKey)
print("The encoded message is:\n", encMessage)


encodMessage = input("Enter an encrypted message to decode:")
print("The following are the decoded messages for keys 1 to 100:")
decrypt(encodMessage)




I am getting this error:
line 10, in decrypt
for encryptChar in range(len(encrpytedString)):
NameError: global name 'encrpytedString' is not defined

I am still pretty new to python but shouldn't encryptedString be available through my entire function, even in my nested for loop? and if not does anyone know a way to extend it's scope into my nested for loop? Thanks!

i removed my info comments from the top. Without them the error would be line 4***

Is This A Good Question/Topic? 0
  • +

Replies To: help with an encrypt/decrypt script

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: help with an encrypt/decrypt script

Posted 02 February 2014 - 11:58 PM

encryptedString will be available, but that's not what you're asking for:

for encryptChar in range(len(encrpytedString)):


encrpytedString is not declared. :)
Was This Post Helpful? 2
  • +
  • -

#3 dakhipp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 06-December 13

Re: help with an encrypt/decrypt script

Posted 03 February 2014 - 12:19 AM

View Postjon.kiparsky, on 02 February 2014 - 11:58 PM, said:

encryptedString will be available, but that's not what you're asking for:

for encryptChar in range(len(encrpytedString)):


encrpytedString is not declared. :)/>


it's supposed to be the argument of my decrypt function and should be encodMessage when I call the function, but I guess it is not available in my nested for loop. is a way to bring it into my nested for loop?

lol miss read your comment and can't delete. Thanks tho!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1