Strings and Text Files (Exercise 1) Dus on Apr 29 a:11 PM PD encrypt.py 1| #Inpo
ID: 3718429 • Letter: S
Question
Strings and Text Files (Exercise 1) Dus on Apr 29 a:11 PM PD encrypt.py 1| #Inport string 2inport string Test Results 013 passcd Test Case Strings and Text Fles Write a script that inputs a line of plaintext and a distance value and outputs an encrypted text using a Caesar cipher. The script should work for any printable characters. 4# define caesar function FAILED: Encryption I 5 def caesar (plainText, distance) distance %-26 INPUT 71 #Encrypting logic 8 | ?0w-string, asc11-lowercase 91 up=strtng. asc11-uppercase 10 11 sf up-up[distance: J+upt:distance] 2alpha-low+up 13 14 CLpherText-str.maketrans(alpha, sf_al) 15 16 17 18 19 ##Prop text from the user 20 platnText-input Enter a text to encrypt:" 21 22 #Prop distance from the user 23 distance-int(nput( Enter a distance value:")) 24 abcde 128 sf low low[dtstance:1+1 +low:distance] OUTPUT sf al-sf_low+sf_up Enter a text to encrypt:abcde Enter a distance value: 128 Encrypted text: yzabc eturn the encrypted value return plainText.translate(cipherText) RESULTS Show Details FAILED: EncrvntionI Run Code Test GradeExplanation / Answer
def caesarEncript(plaintext,distance):
encriptText=""
for i in range(len(plaintext)):
str=plaintext[i]
#chr convert a number to char
#ord convert a number to int
if(str.isupper()):
encriptText+=chr((ord(str) + distance - 65) % 26 + 65)
else: encriptText+=chr((ord(str) + distance - 97) % 26 + 97)
return encriptText
#decripting function
def caesarDecript(plaintext,distance):
encriptText=""
for i in range(len(plaintext)):
str=plaintext[i]
if(str.isupper()):
encriptText+=chr((ord(str) - distance - 65) % 26 + 65)
else: encriptText+=chr((ord(str) - distance - 97) % 26 + 97)
return encriptText
#input from user
plainText=input("Enter a text to encript : ")
distance=int(input("Enter distance : "))
print ("Encript Text : ")
#call encription function
print (caesarEncript(plainText,distance))
plainText=input("Enter a text to Decript : ")
distance=int(input("Enter distance : "))
#call decription function
print ("Decript Text : ")
print(caesarDecript(plainText,distance))