Ok So I missed class and am trying to do the work they did in there. One of the problems was fixing a caeser cipher. I believe I have fixed all except for the last part which is of course the part I'm stuck on. This is what I have.
#Change to T to lowercase in plaintext
#add : to the end of if statement
def encrypt(plaintext, alphabet, key):
plaintext = plaintext.lower()
cipherText = ""
for ch in plaintext:
idx = alphabet.find(ch)
if idx >= 0 and idx <= 25:
cipherText = cipherText + key[idx]
else:
cipherText = cipherText + ch
#add return ciphertext
return cipherText
#add def to decrypt line
def decrypt(cipherText, alphabet, key):
plainText = ""
for ch in cipherText:
idx = key.find(ch)
#add and idx <= 25 to if statement
if idx >= 0 and idx <= 25:
plaintext = plaintext + alphabet[idx]
else:
plaintext = plaintext + ch
return plaintext
#got rid of def main
#have myCipher = encrypt
# define both myCipher and my plain to the encrypt and decrypt
alphabet = "abcdefghijklmnopqrstuvwxyz"
key = "nopqrstuvwxyzabcdefghijklm"
plaintext = input("Enter the text you want to encrypt: " )
myCipher = encrypt(plaintext, alphabet, key)
myPlain = decrypt(cipherText,alphabet, key)
print(myCipher)
print("Checking if decryption works: ")
print(myPlain)
When I run the code it says cipherText is not defined in
myPlain = decrypt(cipherText,alphabet, key)
I have tried a few different options but I seem to be going further from fixing it than what I have it as now. So is a way I can define cipherText in that line or do I have to redo that line and change it to something else?
This is the error I get when I tried to change cipherText as 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 a local variable only within that function...
myCipher = encrypt(plaintext, alphabet, key)
myPlain = decrypt(myCipher ,alphabet, key)