user11235667
user11235667

Reputation: 3

Having multiple replacement of alphabets within the for loop

I'm trying to replace characters in a sentence using a for loop (more like decrypting a message) using giving cipherKey.

I managed to set both orgKey and cipherKey to align and replace each other (please see the code below for clear understanding regarding this).

orgKey = [list]
cipherKey = [list]
message = "some secret message"

for i in range (len(orgKey)):
    message = message.replace(cipherKey[i], orgKey[i])
    print(message)

I was expecting a clean message with replaced letters, but since "message = message.replace(cipherKey[i], orgKey[i]) line is included in for loop, it replaced each letter in the length of orgKey. What would be the best, cleaner method with this?

Upvotes: 0

Views: 46

Answers (1)

try this:

orgKey = [list]
cipherKey = [list]
message = "some secret message"

message_encrypt = ''
dictionary = dict(zip(orgKey, cipherKey))
for char in message:
    message_encrypt += dictionary.get(char,' ')
print(message_encrypt)

Only if the lists have the same length.

Upvotes: 1

Related Questions