print("CryptoPrac-1a.")
for x in range(97, 123): # 97 starts the lowercase alphabet and 122 is z (ending)
print(chr(x), end = ' ')
print('\n')
for x in range(97, 123):
print(chr(x + 3), end = ' ')
print("\n CryptoPrac-1b.")
I am trying to add to the second part of this code. What I am trying to do is keep the shift by three but only this time I am trying to use the modulus operator. I am using the ASCII table but I don't know how to start a for loop to do what I want to do
.
There are better ways to do this:
If you are using python 3.6 or newer:
>>> import string
>>> funk = '{|}'
>>> mystr = f'{string.ascii_lowercase[3:]}{funk}'
>>> mystr = ' '.join(mystr)
>>> mystr
'd e f g h i j k l m n o p q r s t u v w x y z { | }'
>>>
otherwise:
>>> import string
>>> funk = '{|}'
>>> mystr = '{}{}'.format(string.ascii_lowercase[3:], funk)>>> mystr = ' '.join(mystr)
>>> mystr
'd e f g h i j k l m n o p q r s t u v w x y z { | }'
>>>
actually you can combine the join:
>>> import string
>>> funk = '{|}'
>>> mystr = ' '.join(f'{string.ascii_lowercase[3:]}{funk}')
>>> mystr
'd e f g h i j k l m n o p q r s t u v w x y z { | }'
>>>
using:
>>> import string >>> funk = '{|}' >>> mystr = ' '.join(f'{string.ascii_lowercase[3:]}{funk}') >>> mystr 'd e f g h i j k l m n o p q r s t u v w x y z { | }' >>>
won't work.
the >>> is the python interactive interpreter prompt
in code use (for example):
import string
funk = '{|}'
mystr = f'{string.ascii_lowercase[3:]}{funk}'
mystr = ' '.join(mystr)
print(mystr)
'd e f g h i j k l m n o p q r s t u v w x y z { | }'
So I was able to come up with the following code:
def encrypt(text):
result = ""
s = 3
# traverse text
for i in range(len(text)):
char = text[i]
# Encrypt uppercase characters
if (char.isupper()):
result += chr((ord(char) + s-65) % 26 + 65)
# Encrypt lowercase characters
else:
result += chr((ord(char) + s - 97) % 26 + 97)
return result
def decrypt(text):
result = ""
s = 26-3
# traverse text
for i in range(len(text)):
char = text[i]
# Decrypt uppercase characters
if (char.isupper()):
result += chr((ord(char) + s-65) % 26 + 65)
# Decrypt lowercase characters
else:
result += chr((ord(char) + s - 97) % 26 + 97)
return result
#check the above function
text = input("Enter Message Please:")
print("Text : " + text)
cipher = encrypt(text)
print("Cipher: " + cipher)
print("De-Cipher: " + decrypt(cipher))
HOWEVER, the output is:
Output:
Enter Message Please: Hello World!
Text : Hello World!
Cipher: KhoorqZruogr
De-Cipher: HellonWorldo
How can I modify this to have the following output instead?
Output:
Enter Message Please: Hello World!
Text : Hello World!
Cipher: Khoor Zruogr!
De-Cipher: Hello World!