-1

I'm making an encoding and decoding program, right now I'm making the decoding program. I have substituted the entire English alphabet for a different letter (e.g. a = e, b = f, c = g) and I've written code that asks for the user to input the encrypted message using:

encrypted_message = input("Insert the encrypted message")

and I want to make it so the user can type "abc" and python would translate "abc" into "efg" and type it back.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Julian Ceasar
  • 75
  • 1
  • 2

2 Answers2

0

Use a dictionary and then map the user's input to the dictionary's get method to retrieve each value:

>>> d = {'a':'e', 'b':'f', 'c':'g'}
>>> print(*map(d.get, 'cab'), sep='')
gef
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

Use translate() method:

For Python 2.x:

from string import maketrans

encrypted = "abc"                                 # chars to be translated
decrypted = "efg"                                 # their replacements

trantab   = maketrans(originals, encrypted)       # make translation table from them

print encrypted_message.translate( trantab )      # Apply method translate() to user input

For Python 3.x:

encrypted = "abc"                                 # chars to be translated
decrypted = "efg"                                 # their replacements

trantab   = str.maketrans(encrypted, decrypted)   # make translation table from them

print( encrypted_message.translate( trantab ) )   # Apply method translate() to user input
MarianD
  • 13,096
  • 12
  • 42
  • 54