5
\$\begingroup\$

I am fairly new to Python 3, and I was challenged to make a substitution cipher. I came up with a very bad way to do it, but I can't think of a better way to do it.

import string
# Characters used
alpha = list(string.ascii_letters)

#Message to encrypt
message = list(input('message: '))

newMessage = []

for letter in message:
    if letter == 'a':
        letter = 'n'
        newMessage.append(letter)
    elif letter == 'b':
        letter = 'z'
        newMessage.append(letter)
    elif letter == 'c':
        letter = 'g'
        newMessage.append(letter)
    elif letter == 'd':
        letter = 'h'
        newMessage.append(letter)
    elif letter == 'e':
        letter = 'q'
        newMessage.append(letter)
    elif letter == 'f':
        letter = 'k'
        newMessage.append(letter)
    elif letter == 'i':
        letter = 'm'
        newMessage.append(letter)
    elif letter == 'j':
        letter = 'y'
        newMessage.append(letter)
    elif letter == 'l':
        letter = 'o'
        newMessage.append(letter)
    elif letter == 'p':
        letter = 'x'
        newMessage.append(letter)
    elif letter == 'r':
        letter = 'v'
        newMessage.append(letter)
    elif letter == 's':
        letter = 't'
        newMessage.append(letter)
    elif letter == 'u':
        letter = 'w'
        newMessage.append(letter)
    elif letter == 'n':
        letter = 'a'
        newMessage.append(letter)
    elif letter == 'z':
        letter = 'b'
        newMessage.append(letter)
    elif letter == 'g':
        letter = 'c'
        newMessage.append(letter)
    elif letter == 'h':
        letter = 'd'
        newMessage.append(letter)
    elif letter == 'q':
        letter = 'e'
        newMessage.append(letter)
    elif letter == 'k':
        letter = 'f'
        newMessage.append(letter)
    elif letter == 'm':
        letter = 'i'
        newMessage.append(letter)
    elif letter == 'y':
        letter = 'j'
        newMessage.append(letter)
    elif letter == 'o':
        letter = 'l'
        newMessage.append(letter)
    elif letter == 'x':
        letter = 'p'
        newMessage.append(letter)
    elif letter == 'v':
        letter = 'r'
        newMessage.append(letter)
    elif letter == 't':
        letter = 's'
        newMessage.append(letter)
    elif letter == 'w':
        letter = 'u'
        newMessage.append(letter)
    elif letter == 'A':
        letter = 'N'
        newMessage.append(letter)
    elif letter == 'B':
        letter = 'Z'
        newMessage.append(letter)
    elif letter == 'C':
        letter = 'G'
        newMessage.append(letter)
    elif letter == 'D':
        letter = 'H'
        newMessage.append(letter)
    elif letter == 'E':
        letter = 'Q'
        newMessage.append(letter)
    elif letter == 'F':
        letter = 'K'
        newMessage.append(letter)
    elif letter == 'I':
        letter = 'M'
        newMessage.append(letter)
    elif letter == 'J':
        letter = 'Y'
        newMessage.append(letter)
    elif letter == 'L':
        letter = 'O'
        newMessage.append(letter)
    elif letter == 'P':
        letter = 'X'
        newMessage.append(letter)
    elif letter == 'R':
        letter = 'V'
        newMessage.append(letter)
    elif letter == 'S':
        letter = 'T'
        newMessage.append(letter)
    elif letter == 'U':
        letter = 'W'
        newMessage.append(letter)
    elif letter == 'N':
        letter = 'A'
        newMessage.append(letter)
    elif letter == 'Z':
        letter = 'B'
        newMessage.append(letter)
    elif letter == 'G':
        letter = 'C'
        newMessage.append(letter)
    elif letter == 'H':
        letter = 'D'
        newMessage.append(letter)
    elif letter == 'Q':
        letter = 'E'
        newMessage.append(letter)
    elif letter == 'K':
        letter = 'F'
        newMessage.append(letter)
    elif letter == 'M':
        letter = 'I'
        newMessage.append(letter)
    elif letter == 'Y':
        letter = 'J'
        newMessage.append(letter)
    elif letter == 'O':
        letter = 'L'
        newMessage.append(letter)
    elif letter == 'X':
        letter = 'P'
        newMessage.append(letter)
    elif letter == 'V':
        letter = 'R'
        newMessage.append(letter)
    elif letter == 'T':
        letter = 'S'
        newMessage.append(letter)
    elif letter == 'W':
        letter = 'U'
        newMessage.append(letter)
    else:
        newmessage.append(letter)

print(''.join(newMessage))
\$\endgroup\$
8
\$\begingroup\$

ascii letters returns a string, containing lower and uppercase letters. You can iterate over a string directly, so there's no need to convert to a list, for both message and ascii_letters.

You can put the letters in order that you want in another string, following the format of ascii_letters. The, you can use the translate function, as explained here.

from string import ascii_letters

cipher_letters = 'nzghqkcdmyfoialxevtswrupjbNZGHQKCDMYFOIALXEVTSWRUPJB'
trans = str.maketrans(ascii_letters, cipher_letters)

text_to_cipher = input('Text to cipher: ')

ciphered = text_to_cipher.translate(trans)
print(f'Ciphered text: {ciphered}')

You can transfer that into a function too, so you can reuse it.

from string import ascii_letters

cipher_letters = 'nzghqkcdmyfoialxevtswrupjbNZGHQKCDMYFOIALXEVTSWRUPJB'


def ryan_cipher(text):
    trans = str.maketrans(ascii_letters, cipher_letters)
    return text.translate(trans)

if __name__ == '__main__':
    text_to_cipher = input('Text to cipher: ')
    ciphered = ryan_cipher(text_to_cipher)

    print(f'Ciphered text: {ciphered}')

Result:

Text to cipher: Hello, world!
Ciphered text: Dqool, ulvoh!

If you wish to decipher Dqool, ulvoh!, you just need to reverse the direction of the translation table, as such:

def decipher(text):
    trans = str.maketrans(cipher_letters, ascii_letters)
    return text.translate(trans)

Result:

Text to decipher: Dqool, ulvoh!
Deciphered text: Hello, world!
\$\endgroup\$
-2
\$\begingroup\$

I have made an improved version. it can code and decode. and it requires no long lists

def run():
    x=input("code or decode? ")
    if x == "code":
        a=list("")
        import string
        alpha = str("abcdefghijklmnopqrstuvwxyz")
        message=input("What is your message to encode? ")
        x=len(message)
        y=0
        z=0
        for counter in range(x):
            y=y+1
            letter = alpha.find(message[z:y])
            z=z+1
            letter=letter+13
            if letter > 24:
                letter=letter-24
            letter=alpha[letter:letter+1]
            if counter != x-1:
                print(letter,end="")
            else:
                print(letter)
        x=input("again? ")
        if x == "yes":
            run()
        else:
            input()

    else:
        a=list("")
        import string
        alpha = str("abcdefghijklmnopqrstuvwxyz")
        message=input("What is your message to decode? ")
        x=len(message)
        y=0
        z=0
        for counter in range(x):
            y=y+1
            letter = alpha.find(message[z:y])
            z=z+1
            letter=letter-13
            if letter < 0:
                letter=24+letter
            letter=alpha[letter:letter+1]
            if counter != x-1:
                print(letter,end="")
            else:
                print(letter)
        x=input("again? ")
        if x == "yes":
            run()
        else:
            input()

run()
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review! While you have added improved code, you haven't reviewed the code. From the Help Center page How do I write a good answer?: "Every answer must make at least one insightful observation about the code in the question. Answers that merely provide an alternate solution with no explanation or justification do not constitute valid Code Review answers and may be deleted." Please read Why are alternative solutions not welcome? \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Feb 12 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.