티스토리 뷰
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']
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"))
#TODO-1: Create a function called 'encrypt' that takes the 'text' and 'shift' as inputs.
def encrypt(from_text, amount_shift):
# input_txt=list(from_text)
# for i in range(0,len(input_txt)): #입력 테스트 하나씩 확인
# if input_txt[i] in alphabet and ((alphabet.index(input_txt[i]))+amount_shift) <= 26:
# input_txt[i] = alphabet[(alphabet.index(input_txt[i]))+amount_shift]
# elif input_txt[i] in alphabet and ((alphabet.index(input_txt[i]))+amount_shift) > 26:
# input_txt[i] = alphabet[((alphabet.index(input_txt[i]))+amount_shift)%26]
cipher_text=''
for char in from_text: #입력 테스트 하나씩 확인
if char in alphabet and ((alphabet.index(char))+amount_shift) <= 25:
cipher_text += alphabet[(alphabet.index(char))+amount_shift]
elif char in alphabet and ((alphabet.index(char))+amount_shift) > 25:
cipher_text += alphabet[((alphabet.index(char))+amount_shift)%26]
else:
cipher_text += char
#print(f"The encoded text is \'{''.join(input_txt)}\'")
print(f"The encoded text is \'{cipher_text}\'")
#TODO-2: Inside the 'decrypt' function, shift each letter of the 'text' *backwards* in the alphabet by the shift amount and print the decrypted text.
#e.g.
#cipher_text = "mjqqt"
#shift = 5
#plain_text = "hello"
#print output: "The decoded text is hello"
def decrypt(from_cipher_text, amount_shift):
plain_text=''
for char in from_cipher_text: #입력 테스트 하나씩 확인
if char in alphabet and ((alphabet.index(char))-amount_shift) >= 0:
plain_text += alphabet[(alphabet.index(char))-amount_shift]
elif char in alphabet and ((alphabet.index(char))-amount_shift) < 0:
plain_text += alphabet[((alphabet.index(char))-amount_shift)%26]
else:
plain_text += char
#print(f"The decoded text is \'{''.join(input_txt)}\'")
print(f"The decoded text is \'{plain_text}\'")
if direction == "encode" :
encrypt(text, shift)
elif direction == "decode" :
decrypt(text, shift)
else :
print("Error! Type 'encode' to encrypt, type 'decode' to decrypt.")
#TODO-3: Call the encrypt function and pass in the user inputs. You should be able to test the code and encrypt a message.
'Test > Python(20220101~)' 카테고리의 다른 글
Day9_Dictionaries, Nesting (0) | 2022.01.11 |
---|---|
Day8_Caesar 암호 최종, 코드 재구성(하나의 함수로 정리) (0) | 2022.01.10 |
Day8_Caesar 암호 1단계, encoding (0) | 2022.01.09 |
Day8_Prime Number(소수) 감별 (0) | 2022.01.09 |
Day8_페인트 면적 계산 (Function) (0) | 2022.01.09 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- EXA
- set()
- 정렬
- storage
- 읽어오기
- insert
- artandculture
- powershell
- fromkeys
- 대소문자
- exadata
- powercli
- cloud-init
- 3par
- 부동없이
- 변수화
- 중복제거
- dp-1
- 차집합
- dezoomify
- oracle
- 배열
- virt-sysprep
- Join
- LIST
- vmware.powercli
- sysprep
- vmware
- dp-2
- 스토리지
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함