I'm trying to make a simple program that will convert to and from a cipher that's already set.
Couple of things I couldn't get working:
Thanks for your help in advance.
Code:
foo = 'a = hz\nb = ye\nc = uo\nd = pd\ne = qi\nf = jy\ng = ru\nh = sw\ni = ln\nj = ae\nk = na\nl = rt\nm = wu\nn = is\no = nd\np = hw\nq = op\nr = kb\ns = vf\nt = fc\nu = xr\nv = ex\nw = mn\nx = gb\ny = bu\nz = ie\n. = *right wing slash* \n, = *hand wave* \n" = *hand curve* \n! = *double hand jerk* \n? = *wing wave* \n... = *squiggly hand motion* '
repl={}
reverse_repl={}
import sys
#Generate replacement tables
for str in foo.split("\n"):
parts=str.split(" = ")
repl[parts[0]]=parts[1]
repl[parts[0].lower()]=parts[1]
reverse_repl[parts[1]]=parts[0]
def encipher():
outstr = ''
instr = sys.stdin.readline()
i = 0
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
print outstr
def decipher():
outstr = ''
instr = sys.stdin.readline()
instr=instr[:-1]
i = 0
c = instr[i]
while (i<len(instr)):
if (instr[i:i+2]):
outstr+=reverse_repl[instr[i:i+2]]
i+=2
else:
outstr+=c
i+=1
print outstr
if (sys.argv[1]=="-e"):
encipher()
else:
decipher()
raw_input('press Return to end program>')
- I couldn't separate the '...' from the '.' The '...' will give the correct equivalent from the list when enciphering, but will also follow it with 2 instances of '.'
- For deciphering, it works up until it goes up against a character that's not in "foo" like 123 or a space.
Thanks for your help in advance.
Comment