Issue
class CipherTest:
def __init__(self):
self.shift = 0
self.direction = 'r'
self.text = "Testing"
# Shift to right function
def shift_to_right(self, text, shift):
encrypted_text = ""
for i in range(len(self.text)):
c = self.text[i]
# Encrypt upper case
if (c == ' '):
encrypted_text += ' '
elif (c.isupper()):
encrypted_text += chr((ord(c) + self.shift - 65) % 26 + 65)
# Encrypt lower case
else:
encrypted_text += chr((ord(c) + self.shift - 97) % 26 + 97)
return encrypted_text
# Shift to left function
def shift_to_left(self, text, shift):
encrypted_text = ""
for i in range(len(self.text)):
c = self.text[i]
# Encrypt upper case
if (c == ' '):
encrypted_text += ' '
elif (c.isupper()):
encrypted_text += chr((ord(c) - self.shift - 65) % 26 + 65)
# Encrypt lower case
else:
encrypted_text += chr((ord(c) - self.shift - 97) % 26 + 97)
return encrypted_text
if __name__ == "__main__":
user_text = str(input())
user_shift = int(input())
user_direction = str(input().lower()) # user inputs
Cipher_Message = CipherTest() # create an instance of the class
if user_direction == 'l': # picking left or right
print(CipherTest.shift_to_left(Cipher_Message, user_text, user_shift))
if user_direction == 'r':
print(CipherTest.shift_to_right(Cipher_Message, user_text, user_shift))
Am I calling the functions within my class incorrectly? Currently no matter the input, it prints "Testing". I'm new to classes but I believe that in the last couple lines, I am calling the new instance, telling it which method to perform, then feeding it the variables it needs to successfully run. From there it should be printing the 'encrypted_text' that is returned using my methods.
Solution
The issue is that CipherTest
have attributes direction, shift and text but you also have local variables that you pass as parameter which you don't use
So your method keep using Testing
and 0
, that returns Testing
so
The easiest, is to remove the attributs, so the class instance can do both direction, and in fact the method can be static (meaning no instance needed, only the class)
class CipherTest:
@staticmethod
def shift_to_right(text, shift):
encrypted_text = ""
for c in text:
if c == ' ':
encrypted_text += ' '
elif c.isupper():
encrypted_text += chr((ord(c) + shift - 65) % 26 + 65)
else:
encrypted_text += chr((ord(c) + shift - 97) % 26 + 97)
return encrypted_text
if __name__ == "__main__":
user_text = "hey how are you"
user_shift = 2
user_direction = "l"
if user_direction == 'l':
enc = CipherTest.shift_to_left(user_text, user_shift)
print(enc) # encrypted
print(CipherTest.shift_to_right(enc, user_shift)) # back to plain
elif user_direction == 'r':
enc = CipherTest.shift_to_right(user_text, user_shift)
print(enc) # encrypted
print(CipherTest.shift_to_left(enc, user_shift)) # back to plain
Another example, so you understand better, is to put the shift
as class attribut, in order to use ise, we need to save in the class with self.shift = shift
then we can use one cipher instance, with it's shift, to do both operation (also see how we really call a instance method)
class CipherTest:
def __init__(self, shift: int):
self.shift = shift
def shift_to_right(self, text):
encrypted_text = ""
for c in text:
if c == ' ':
encrypted_text += ' '
elif c.isupper():
encrypted_text += chr((ord(c) + self.shift - 65) % 26 + 65)
else:
encrypted_text += chr((ord(c) + self.shift - 97) % 26 + 97)
return encrypted_text
if __name__ == "__main__":
user_text = "hey how are you"
user_shift = 2
user_direction = "l"
cipher = CipherTest(user_shift)
if user_direction == 'l':
enc = cipher.shift_to_left(user_text)
print(enc) # encrypted
print(cipher.shift_to_right(enc)) # back to plain
elif user_direction == 'r':
enc = cipher.shift_to_right(user_text)
print(enc) # encrypted
print(cipher.shift_to_left(enc)) # back to plain
Answered By - azro Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.