alphabet = []
def Init():
idx = 0
for i in range(97, 123):
alphabet.append(chr(i))
def Encrypt(plainTxt, shiftAmt, code = 0):
cipherTxt = ""
for letter in plainTxt:
pos = alphabet.index(letter)
newPos = pos + shiftAmt if code == 0 else pos - shiftAmt
cipherTxt += alphabet[newPos]
return cipherTxt
def Decrypt(plainTxt, shiftAmt):
return Encrypt(plainTxt, shiftAmt, 1)
Init()
dir = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
if not dir == 'encode' and not dir == 'decode':
print("Try Again")
exit()
txt = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
cipherTxt = Encrypt(txt, shift) if dir == "encode" else Decrypt(txt, shift)
print(f"The new text is : {cipherTxt}")
'프로그래밍 언어 > Python' 카테고리의 다른 글
[Python] 포메팅 format (0) | 2024.07.31 |
---|---|
[Python] enum형 (0) | 2024.07.16 |
[Python] 비슷한 연산자의 차이 (is, ==, and, &, or, |) (0) | 2024.07.09 |
[Python] 문자열 관련 함수 총 정리 (0) | 2024.06.14 |
[Python] f-string (문자열) (0) | 2024.03.21 |