0
def decoder(message):
    key = {'a':'n', 'b':'o', 'c':'p', 'd':'q', 'e':'r', 'f':'s', 'g':'t', 'h':'u', 
   'i':'v', 'j':'w', 'k':'x', 'l':'y', 'm':'z', 'n':'a', 'o':'b', 'p':'c', 
   'q':'d', 'r':'e', 's':'f', 't':'g', 'u':'h', 'v':'i', 'w':'j', 'x':'k',
   'y':'l', 'z':'m', 'A':'N', 'B':'O', 'C':'P', 'D':'Q', 'E':'R', 'F':'S', 
   'G':'T', 'H':'U', 'I':'V', 'J':'W', 'K':'X', 'L':'Y', 'M':'Z', 'N':'A', 
   'O':'B', 'P':'C', 'Q':'D', 'R':'E', 'S':'F', 'T':'G', 'U':'H', 'V':'I', 
   'W':'J', 'X':'K', 'Y':'L', 'Z':'M'} 
    for i in message:
        for x,y in key.items():
            if i == y:
                message = message.replace(y,x)
   return message

>>>decoder('Pnrfne pvcure? V zhpu cersre Pnrfne fnynq!')
'Carsar piphrr? I mhph prrsrr Carsar salad!'

Only some of the letters get translated and I cannot work out why. Can anyone spot why this is?

0

2 Answers 2

1

You're replacing things multiple times. I think:

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

print(decoder('Pnrfne pvcure? V zhpu cersre Pnrfne fnynq!'))

is probably what you wanted (prints Caesar cipher? I much prefer Caesar salad!).

Sign up to request clarification or add additional context in comments.

3 Comments

One line is all python needs
Any idea why this code does not work on the following: trans={"god":"merry", "jul":"christmas", "och":"and", "gott":"happy", "nytt":"new", "år":"year"} print(''.join([trans[a] if a in trans else a for a in 'Festive greeting: god jul och gott nytt år']))
The for a in ... is iterating through the string on a character-by-character basis, whereas now you want to convert susbstrings of your original string.
1

Better change (or rebuild) the dictionary to fit str.translate method (which needs ascii code of the letter as key), done just for that:

key = {'a':'n', 'b':'o', 'c':'p', 'd':'q', 'e':'r', 'f':'s', 'g':'t', 'h':'u',
   'i':'v', 'j':'w', 'k':'x', 'l':'y', 'm':'z', 'n':'a', 'o':'b', 'p':'c',
   'q':'d', 'r':'e', 's':'f', 't':'g', 'u':'h', 'v':'i', 'w':'j', 'x':'k',
   'y':'l', 'z':'m', 'A':'N', 'B':'O', 'C':'P', 'D':'Q', 'E':'R', 'F':'S',
   'G':'T', 'H':'U', 'I':'V', 'J':'W', 'K':'X', 'L':'Y', 'M':'Z', 'N':'A',
   'O':'B', 'P':'C', 'Q':'D', 'R':'E', 'S':'F', 'T':'G', 'U':'H', 'V':'I',
   'W':'J', 'X':'K', 'Y':'L', 'Z':'M'}

# we can rebuild it like that
newkey = {ord(k):v for k,v in key.items()}

def decoder(s):
    return s.translate(newkey)

print( decoder('Pnrfne pvcure? V zhpu cersre Pnrfne fnynq!') )

result:

Caesar cipher? I much prefer Caesar salad!

all characters not in dictionary are left as-is.

next simplest thing without that would be: "".join([key.get(i,i) for i in s])

(using dict.get with default as input if not found)

In that particular case, there's an even simpler solution using codecs and rot13 encoding:

import codecs
def decoder(s):
    return codecs.encode(s,"rot13")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.