Как реализовать шифрование и дешифрование ceaser cipher в одной функции?
Hi Below are my codes for encryption and decryption. import ast a = ast.literal_eval(input()) n1 = a[0] n2 = a[1] def encryption(string,step): result = '' for i in string: if i == "": result = result+i elif i.isupper(): result = result + chr((ord(i) + step - 65) % 26 + 65) else: result = result + chr((ord(i) + step - 97) % 26 + 97) return result print(encryption(n1,n2)) Input: n1 = upHrae, n2 = 4 Output: ytLvei #Program for Decryption def decryption(string1,step): result1 = '' for j in string1: if j == "": result1 = result1+i elif j.isupper(): result1 = result1 + chr((ord(i) + step + 65) % 26 + 65) else: result1 = result1 + chr((ord(i) + step + 97) % 26 + 97) return result1 print(drcryption(n1,n2)) input: n1 = banana, n2 = 7 output: utgtgt I want to combine both encryption and decryption function in one function which generates output as 'ytLvei' when input is 'n1 = upHrae, n2 = 4' and generates output as 'utgtgt' when input is 'n1 = banana, n2 = 7.' Please help me.
Что я уже пробовал:
I tried Below code but it is not working as expected. import ast n = ast.literal_eval(input()) n1 = n[0] step = n[1] def enc_dec(string,step): result = '' for i in string: if ((ord(i) + step)>=65) or ((ord(i) + step)<=97): if i=='': result = result+i elif i.isupper(): result = result + chr((ord(i) + step - 65) % 26 + 65) else: result = result + chr((ord(i) + step - 97) % 26 + 97) if ((ord(i) + step)>=97) or ((ord(i) + step)<=122): if i=='': result = result+i elif i.isupper(): result = result + chr((ord(i) + step + 65) % 26 + 65) else: result = result + chr((ord(i) + step + 97) % 26 + 97) return result print(enc_dec(n1,step))
Richard Deeming
if ((ord(i) + step)>=65) or ((ord(i) + step)<=97):
Это похоже на ошибку в вашем коде. Можете ли вы назвать одно число, которое не больше или равно
65
ОПЕРАЦИОННАЯ меньше или равно 97
?