I am to summarise just using Caesar cipher on my python program to encrypt and decrypt. However, I seem to be completely lost because I want to return self.cipher which is the encrypted message. I want to be able to store this value and use it to decrypt it in my other method decryptCipher. I need advice on how to pass the value I return from the encryptCipher method to the method decryptCipher. I also from running this get an error saying 'str is not callable'. Thanks!
class CaesarCipher(object):
def __init__(self, plainTxt, key, alphabet):
self.plainTxt = plainTxt
self.key = key
self.alphabet = alphabet
self.cipher = " "
self.decryptCipher = " "
def encryptCipher(self):
text = self.plainTxt.lower()
for lettersintext in text:
if lettersintext == " ":
self.cipher+=" "
else:
for i in range(len(self.alphabet)):
if self.alphabet[i] == lettersintext:
self.cipher += self.alphabet[i+self.key]
break
return self.cipher
def decryptCipher(self, encryptedVer):
#request if want to decrypt
text = encryptedVer.lower()
for lettersintext in text:
if lettersintext == " ":
self.decryptCipher+=" "
else:
for i in range(len(alphabet)):
if self.alphabet[i] == lettersintext:
self.decryptCipher += self.alphabet[i-self.key]
return self.decryptCipher
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z']
plainTxt = input("Enter a text to encrypt: ")
key = int(input("Key shift value: "))
init = CaesarCipher(plainTxt, key, alphabet)
print(init.encryptCipher())
encryptedVer = init.encryptCipher()
print(init.decryptCipher(encryptedVer))
Also I apologise about the syntax layout. When I copied the code it ended up this way and I am new to this :)
You're storing the encrypted message in both the object (self.cipher) as well as returning it. Not a problem if you want both, but you may want to blank out your self.cipher before you start populating it (otherwise you'll append a whole new encrypted message on top of previous encryptions as you go).
Alternatively, make a temp empty string and append on it all within your method.
In terms of str not callable, it is because you define an attribute self.decryptCipher = " " in init as well as define a method decryptCipher().
your method call is actually calling your string and trying to pass info into it.