0

So I'm creating a program that allows you to set each letter in the alphabet to another one using a dictionary. It then lets you input a sentence, which it then codes using the code you previously set. So far, I've completed (or I think I've completed) everything but the function that replaces the letters, because I have no idea what to do there. Any suggestions? Here's the code:

import sys

def defineAlphabet():
    alphabet = dict()
    alphabet['a'] = input('a = ')
    alphabet['b'] = input('b = ')
    alphabet['c'] = input('c = ')
    alphabet['d'] = input('d = ')
    alphabet['e'] = input('e = ')
    alphabet['f'] = input('f = ')
    alphabet['g'] = input('g = ')
    alphabet['h'] = input('h = ')
    alphabet['i'] = input('i = ')
    alphabet['j'] = input('j = ')
    alphabet['k'] = input('k = ')
    alphabet['l'] = input('l = ')
    alphabet['m'] = input('m = ')
    alphabet['n'] = input('n = ')
    alphabet['o'] = input('o = ')
    alphabet['p'] = input('p = ')
    alphabet['q'] = input('q = ')
    alphabet['r'] = input('r = ')
    alphabet['s'] = input('s = ')
    alphabet['t'] = input('t = ')
    alphabet['u'] = input('u = ')
    alphabet['v'] = input('v = ')
    alphabet['w'] = input('w = ')
    alphabet['x'] = input('x = ')
    alphabet['y'] = input('y = ')
    alphabet['z'] = input('z = ')
    return alphabet

def codeSentence(sentence):
    global translation
    translation = 'WIP'
    return translation

def menu():
    print('''Would you like to:
a. Code a sentence
b. Set the code
c. Quit''')
    userInput = input('//> ')
    if userInput == 'a':
        codeSentence(input('Enter Sentence: '))
        print(translation)
        menu()
    if userInput == 'b':
        defineAlphabet()
        print('Defined!')
        menu()
    if userInput == 'c':
        print('Goodbye!')
        sys.exit(0)
    else:
        print('That is not an option.')
        menu()
menu()

1 Answers1

0
result = "some sentence".translate({ord(k): v for k, v in alphabet.items()})

See str.translate().

jfs
  • 399,953
  • 195
  • 994
  • 1,670