0

I have this program where I want to convert a normal message, to an encrypted one. In the program, it replaces each letter with it's opposite. When it changes from c = c.replace('n','m') then it replaces all of the ones it replaced before.

def convert(c):
    c = c.replace('a', 'z')
    c = c.replace('b', 'y')
    c = c.replace('c', 'x')
    c = c.replace('d', 'w')
    c = c.replace('e', 'v')
    c = c.replace('f', 'u')
    c = c.replace('g', 't')
    c = c.replace('h', 's')
    c = c.replace('i', 'r')
    c = c.replace('j', 'q')
    c = c.replace('k', 'p')
    c = c.replace('l', 'o')
    c = c.replace('m', 'n')
    c = c.replace('n', 'm')
    c = c.replace('o', 'l')
    c = c.replace('p', 'k')
    c = c.replace('q', 'j')
    c = c.replace('r', 'i')
    c = c.replace('s', 'h')
    c = c.replace('t', 'g')
    c = c.replace('u', 'f')
    c = c.replace('v', 'e')
    c = c.replace('w', 'd')
    c = c.replace('x', 'c')
    c = c.replace('y', 'b')
    c = c.replace('z', 'a')
    return c
print(convert('the quick brown fox jumps over the lazy dog'))

And the output is :

ghe jfick bildm flc jfmkh leei ghe laab dlg

It doesn't convert all the letters. Can someone please help??

2
  • 1
    Letters before the middle of the alphabet will get changed to letters beyond the middle, and will therefore get changed back again. Letters after the middle won't have a chance to get changed back. e.g. h -> s -> h, but t -> g Commented Sep 23, 2018 at 21:55
  • You replace 's' by 'h' and then 'h' by 's', for example. Also, 'l' by 'o' and then 'o' be 'l' right away, and so on and so forth. Commented Sep 23, 2018 at 21:57

2 Answers 2

4

you should use str.translate() as it accepts a mapping and applies all chars at once:

def convert(c):
    return c.translate(mapping)

You can make your mapping like this:

mapping = {
    ord('a'): 'z',
    ord('b'): 'y',
    ...
}

But luckly python also haso the maketrans() function to help you make this mapping:

mapping = str.maketrans(string.ascii_lowercase, string.ascii_lowercase[::-1])
Sign up to request clarification or add additional context in comments.

Comments

1

Let's step through what's happening in this code. Pretend c is the string "yabz."

c = "yabz"

Replace all instances of "a" with "z."

c = "yzbz"

Replace all instances of "b" with "y."

c = "yzyz"

Now, when you begin replacing "y" and "z," some of the letters change back to what they started as:

c = c.replace("y","b") #c is now "bzbz"
c = c.replace("z","a") #c is now "baba"

As a result, you convert letters in the first half of the alphabet to the letters in the second half correctly, but you accidentally change them back. You can see from your output that no letter in the second half of the alphabet shows up because of this. Since this looks like an exercise, you should try to solve the problem yourself, but here is a possible solution:

s = "I am noob"
import string   
letters = string.ascii_lowercase
# construct a dictionary mapping from a letter to its dual opposite starting from the end
# of the alphabet table
rep_dict = dict(zip(letters, letters[::-1]))
# use the dictionary to replace the letters
''.join(map(rep_dict.get, s.replace(" ", "").lower()))
# 'rznmlly'

Source: https://stackoverflow.com/a/42048527/8084393

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.