[146] 시프트코드

Grace Ryu ㅣ 2023. 10. 5. 19:22

1) 입력과 데이터 출력
2) if문
3) 리스트
4) 루프 (while, for)
5) 문자열 나누기, 결합 
6) 함수


#146 

종료하기 전까지 메세지를 인코딩 또는 디코딩 한 다음에 메뉴가 다시 표시되어야 한다.

1) make a code   #숫자입력
2) decode a message   #인코딩된 메세지와 올바른 숫자 입력해야 디코딩된 메세지 출력, 문자를 입력한 숫자만큼 뒤로 이동하여 원래의 문자를 찾도록 한다. 
3) quit   #프로그램 중지 

enter your selection:
 

ver1. 

# 알파벳 리스트 정의, 끝에 공란 추가
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', ' ']


def get_data():
    word = input("메세지 입력: ")
    word = word.lower()  # 소문자
    num = int(input("1~26 사이의 숫자 입력: "))

    while num > 26 or num == 0:
        num = int(input("1~26 사이 숫자 입력: "))
    
    data = (word, num)  # 입력값 튜플
    return data


#인코딩
def make_code(word, num):
    new_word = ''
    for i in word:
        y = alphabet.index(i)
        y = y + num
        if y > 26:
            y = y - 27  
        char = alphabet[y]
        new_word = new_word + char
    print(new_word)  
    print()


# 디코딩
def decode(word, num):
    new_word = ""
    for x in word:
        y = alphabet.index(x)
        y = y - num
        if y < 0:
            y = y + 27  
        char = alphabet[y]
        new_word = new_word + char
    print(new_word)  
    print()


# 메인
def main():
    again = True
    while again == True:
        print("1) 코드 만들기")
        print("2) 메시지 디코딩")
        print("3) 종료")
        print()

        selection = int(input("메뉴 선택: "))
        if selection == 1:
            word, num = get_data()
            make_code(word, num)  # 인코딩
        elif selection == 2:
            word, num = get_data()
            decode(word, num)  # 디코딩
        elif selection == 3:
            again = False  # 루프를 종료
        else:
            print("잘못 입력하였습니다, \n")


main()

 
 

ver2.

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", " "]

def encoding():
    ecd = []
    msg = input("인코딩할 문장을 입력하세요: ")
    num = int(input("인코딩할 숫자를 입력하세요: "))
    for letter in msg.lower():
        idx = alphabet.index(letter)
        if idx + num > len(alphabet) - 1:
            ecd.append(alphabet[(idx + num) - len(alphabet)])
        else: 
            ecd.append(alphabet[idx + num])
    ecd_msg = "".join(ecd)
    print(ecd_msg)
 
def decoding():
    dcd = []
    msg = input("디코딩할 문장을 입력하세요: ")
    num = int(input("디코딩할 숫자를 입력하세요: "))
    for letter in msg.lower():
        if letter != " ":
            idx = alphabet.index(letter)
            if idx - num < 0:
                dcd.append(alphabet[(idx - num) + len(alphabet)])
            else: 
                dcd.append(alphabet[idx - num])
        else:
            dcd.append(" ")
    dcd_msg = "".join(dcd)
    print(dcd_msg)


ans = 0
while ans != 3:
    print("\n1) Make a code")
    print("2) Decode a message")
    print("3) Quit\n")
    ans = int(input("Enter your selection: "))
    if ans == 1:
        encoding()
    elif ans == 2:
        decoding()
    elif ans == 3:
        pass
    else:
        print("잘못된 입력입니다.")
print("프로그램을 종료합니다.")

 
 

ver3.

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', ' ']

def get_data():
    word = input("Enter your message: ")
    word = word.lower() #소문자로 변환
    num = int(input("Enter a number (1-26): "))
    if num > 26 or num == 0 :
        num = int(input("Error, Enter a number (1-26): "))
    data = (word, num) 
    return(data)

def make_code(word, num):
    new_word = ""
    for i in word:
        y = alphabet.index(i)
        y = y + num
        if y > 26 :
            y = y - 27
        char = alphabet[y]
        new_word = new_word + char
    print(new_word)
    print()

def decode(word, num) :
    new_word = ""
    for i in word:
        y = alphabet.index(i)
        y = y - num
        if y < 0 :
            y = y + 27
        char = alphabet[y]
        new_word = new_word + char
    print(new_word)
    print()

def main() :
    again = True
    while again == True :
        print("1) Make a code")
        print("2) Decode a message")
        print("3) Quit")
        print()
        select = int(input("Select: "))
        if select == 1 :
            (word, num) = get_data()
            make_code(word, num)
        elif select == 2 :
            (word, num) = get_data()
            decode(word, num)
        elif select == 3 :
            again = False
        else :
            print("Incorrect")

main()

'Python challenge' 카테고리의 다른 글

[148] 비밀번호  (0) 2023.10.19
[147] Mastermind  (0) 2023.10.19
[139~145] SQLite  (1) 2023.10.03
[124~132] Tkinter GUI  (1) 2023.10.01
Do you think that is possible?  (0) 2023.09.24