Write a python script that inputs a line of plaintext and a distance value and o
ID: 3593790 • Letter: W
Question
Write a python 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.
*the fist input is abcde, and 128. output should show "abcde".
*the second input is abcde, and 100. output should show EFGHI.
*the third input is Hello World, and 3. output should show Khoor#Zruog
all inputs need to output what is shown, and not one or the other. Please comment the sections's for better understanding, as to what the strings are doing.
Explanation / Answer
# take input from user
plainText = input("Enter plain text to encrypt: ")
distance = int(input("Enter distance: "))
# python provide extra ' ' at end when read a input, remove that character
plainText = plainText.rstrip()
# initialize cipher
cipher = ""
# for every charcter in plain text convert to cipher
for c in plainText:
# get charcter position of c
pos = ord(c)
# change position by adding distance and taking module with 128 to keep it in visible range
newPos = (pos + distance)%128
# get char at this new positiom
newChar = chr(newPos)
# add this char to cipher text
cipher += newChar
# print cipher text
print(cipher)
# copy pastable link: https://paste.ee/p/hJnHH
'''
Sample output
Enter plain text to encrypt: abcde
Enter distance:3
EFGHI
'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.