티스토리 뷰
# encode 를 선택하고, 텍스트 메세지를 입력한 후 shift할 양을 넣으면,
# shift되어 암호화 된 텍스트가 출력된다.
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, from_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]))+from_shift) <= 26:
# input_txt[i] = alphabet[(alphabet.index(input_txt[i]))+from_shift]
# elif input_txt[i] in alphabet and ((alphabet.index(input_txt[i]))+from_shift) > 26:
# input_txt[i] = alphabet[((alphabet.index(input_txt[i]))+from_shift)%26]
cipher_text=''
for char in from_text: #입력 테스트 하나씩 확인
if char in alphabet and ((alphabet.index(char))+from_shift) <= 25:
cipher_text += alphabet[(alphabet.index(char))+from_shift]
elif char in alphabet and ((alphabet.index(char))+from_shift) > 25: # shift해서 알파벳 z를 넘게 되면 a부터로 되돌아가게 %나머지 계산을 생각하게 되었음
cipher_text += alphabet[((alphabet.index(char))+from_shift)%26]
else:
cipher_text += char
#print(f"The encoded text is \'{''.join(input_txt)}\'")
print(f"The encoded text is \'{cipher_text}\'")
encrypt(text,shift)
#TODO-2: Inside the 'encrypt' function, shift each letter of the 'text' forwards in the alphabet by the shift amount and print the encrypted text.
#e.g.
#plain_text = "hello"
#shift = 5
#cipher_text = "mjqqt"
#print output: "The encoded text is mjqqt"
##HINT: How do you get the index of an item in a list:
#https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list
##?Bug alert: What happens if you try to encode the word 'civilization'??
#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~)' 카테고리의 다른 글
Day8_Caesar 암호 최종, 코드 재구성(하나의 함수로 정리) (0) | 2022.01.10 |
---|---|
Day8_Caesar 암호 2단계, decode (0) | 2022.01.10 |
Day8_Prime Number(소수) 감별 (0) | 2022.01.09 |
Day8_페인트 면적 계산 (Function) (0) | 2022.01.09 |
Day8_입력값이 있는 함수 (0) | 2022.01.09 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- EXA
- 배열
- dp-1
- storage
- vmware.powercli
- insert
- 부동없이
- 정렬
- LIST
- artandculture
- dp-2
- exadata
- powercli
- 중복제거
- 읽어오기
- Join
- sysprep
- oracle
- 변수화
- fromkeys
- dezoomify
- powershell
- 차집합
- 스토리지
- vmware
- virt-sysprep
- set()
- 3par
- cloud-init
- 대소문자
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함