I've been playing around with encoding random sets of strings using a dictionary. I've gotten my code to replace the letters I want, but in some cases it will replace a character more than once, when I truly only want it to replace the letter in the string once. This is what I have:
def encode(msg,code):
for i in msg:
for i in code:
msg = msg.replace(i, code[i])
return msg
for testing purposes I used the function calls: initial:
encode("blagh", {"a":"e","h":"r"})
and a more complex string:
encode("once upon a time",{'a':'ae','e':'ei','i':'io','o':'ou','u':'ua'})
for the second one right above, I'm looking for the output of : 'ouncei uapoun ae tiomei'
but instead am finding myself with :
"ounceio uapoun aeio tiomeio"
How can I limit my loop to only replacing each character once?