Test/Python(20220101~)
Day8_Caesar 암호 최종, 코드 재구성(하나의 함수로 정리)
kiostory
2022. 1. 10. 21:41
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
import art #from art import logo
print(art.logo)
def caesar(text,shift,direction):
output_text=''
if direction == "encode":
for char in text:
if char in alphabet and ((alphabet.index(char))+shift) <= 25:
output_text += alphabet[(alphabet.index(char))+shift]
elif char in alphabet and ((alphabet.index(char))+shift) > 25:
output_text += alphabet[((alphabet.index(char))+shift)%26]
else:
output_text += char
#print(f"The {direction}ed text is \'{''.join(input_txt)}\'.")
print(f"The {direction}ed text is \'{output_text}\'.")
elif direction == "decode":
for char in text:
if char in alphabet and ((alphabet.index(char))-shift) >= 0:
output_text += alphabet[(alphabet.index(char))-shift]
elif char in alphabet and ((alphabet.index(char))-shift) < 0:
output_text += alphabet[((alphabet.index(char))-shift)%26]
else:
output_text += char
print(f"The {direction}ed text is \'{output_text}\'.")
else :
print("Error! Type 'encode' to encrypt, type 'decode' to decrypt.")
repeat = True
while repeat:
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
caesar(text,shift,direction)
restart = input("Type 'yes if you want to go again. Otherwise type 'no'.\n")
if restart == "no":
repeat = False
print("Goodbye")