python 3.x - how to define decryption of caeser cipher -
ok missed class , trying work did in there. 1 of problems fixing caeser cipher. believe have fixed except last part of course part i'm stuck on. have.
#change t lowercase in plaintext #add : end of if statement def encrypt(plaintext, alphabet, key): plaintext = plaintext.lower() ciphertext = "" ch in plaintext: idx = alphabet.find(ch) if idx >= 0 , idx <= 25: ciphertext = ciphertext + key[idx] else: ciphertext = ciphertext + ch #add return ciphertext return ciphertext #add def decrypt line def decrypt(ciphertext, alphabet, key): plaintext = "" ch in ciphertext: idx = key.find(ch) #add , idx <= 25 if statement if idx >= 0 , idx <= 25: plaintext = plaintext + alphabet[idx] else: plaintext = plaintext + ch return plaintext #got rid of def main #have mycipher = encrypt # define both mycipher , plain encrypt , decrypt alphabet = "abcdefghijklmnopqrstuvwxyz" key = "nopqrstuvwxyzabcdefghijklm" plaintext = input("enter text want encrypt: " ) mycipher = encrypt(plaintext, alphabet, key) myplain = decrypt(ciphertext,alphabet, key) print(mycipher) print("checking if decryption works: ") print(myplain)
when run code says ciphertext not defined in
myplain = decrypt(ciphertext,alphabet, key)
i have tried few different options seem going further fixing have now. way can define ciphertext in line or have redo line , change else?
this error when tried change ciphertext laloldublin suggested
traceback (most recent call last): file "c:\users\david\downloads\caeser (2).py", line 32, in <module> myplain = decrypt(mycipher ,alphabet, key) file "c:\users\david\downloads\caeser (2).py", line 21, in decrypt plaintext = plaintext + alphabet[idx] unboundlocalerror: local variable 'plaintext' referenced before assignment
you can't use ciphertext it's local variable within function...
mycipher = encrypt(plaintext, alphabet, key) myplain = decrypt(mycipher ,alphabet, key)
Comments
Post a Comment