It appears that you want to replace each character with a unique 2 character string with a special exception for "...". I don't understand what
*squiggly hand motion* is, but I'll assume that it is 2 characters. If a character is not in
repl, double it so we be consistent.
Your code:
- for i in range(len(instr)):
-
c = instr[i]
-
if (instr[i:i+3]=='...'):
-
outstr+=repl['...']
-
i+=3
-
else:
-
if (c in repl):
-
outstr+=repl[c]
-
i+=1
-
else:
-
outstr+=c
-
i+=1
Variable
i is updated each iteration. If you assign
i in the body of the loop, you will lose the assignment.
Try the following instead:
- while i < len(instr):
-
c = instr[i]
-
if instr[i:i+3] == '...':
-
outstr += repl['...']
-
i+=3
-
elif c in repl:
-
outstr += repl[c]
-
i+=1
-
else:
-
outstr += c*2
-
i+=1
To get the code to work, I replaced your code:
- while (i<len(instr)):
-
if (instr[i:i+2]):
-
outstr+=reverse_repl[instr[i:i+2]]
-
i+=2
-
else:
-
outstr+=c
-
i+=1
with the following code:
- while i<len(instr):
-
if reverse_repl.has_key(instr[i:i+2]):
-
outstr += reverse_repl[instr[i:i+2]]
-
else:
-
outstr += instr[i][0]
-
i+=2
There were several places in your code where parentheses were not required. Avoid using
str as a variable name, because the built-in function
str() will be masked.
HTH --BV