Programming-Challenges/03 Strings/exercise-03.py

39 lines
1.2 KiB
Python

# Prompt user to enter a key number between 1 and 26
# output the original string and encrypted string
print(r"""
_________
\_ ___ \_____ ____ ___________ _______
/ \ \/\__ \ _/ __ \ / ___/\__ \\_ __ \
\ \____/ __ \\ ___/ \___ \ / __ \| | \/
\______ (____ /\___ >____ >(____ /__|
\/ \/ \/ \/ \/
""")
original = """
If he had anything confidential to say,
he wrote it in cipher,
that is, by so changing the order of the letters of the alphabet,
that not a word could be made out.
"""
key = int(input("Please enter a key number between 1 and 26: "))
if key < 1 or key > 26:
print("Please enter a key number between 1 and 26")
exit()
encrypted = ""
for i in range(len(original)):
if original[i].isalpha():
if original[i].isupper():
encrypted += chr((ord(original[i].lower()) - ord('a') + key) % 26 + ord('A'))
else:
encrypted += chr((ord(original[i].lower()) - ord('a') + key) % 26 + ord('a'))
else:
encrypted += original[i]
print(f"The original string is: {original}")
print(f"\nThe encrypted string is: {encrypted}")