Changing numbers into characters using dictionaries

D

Danny

Hello again,

I am now trying to make something to change some "encrypted" text into
some plain text, here is the code I have so far:

text = '@7704@7002@7075@7704' // some text
num = '213654' // Number
s1 = '700'
s2 = '770'
s4 = '707' // it adds these later on.
t = text.split('@') // splits the digits/blocks apart from each other
a = {s2+num[3]:"l", s1+num[0]:"a", s4+num[5]:"w"}

something = 1
while True:
var = str(a[t[something]])
print var,
// I want it to change "@7704@7002@7075@7704" into "lawl"

I get the error:
Traceback (most recent call last):
File "C:/Documents and Settings/Danny/My
Documents/python/changetext.py", line 9, in ?
var = str(a[t[something]])
KeyError: '7704'

I've explained what is needed to happen in the comments. Also, if any of
you can think of a better way to do this can you possibly tell me this?
Thanks.
 
L

Larry Bates

Danny said:
Hello again,

I am now trying to make something to change some "encrypted" text into
some plain text, here is the code I have so far:

text = '@7704@7002@7075@7704' // some text
num = '213654' // Number
s1 = '700'
s2 = '770'
s4 = '707' // it adds these later on.
t = text.split('@') // splits the digits/blocks apart from each other
a = {s2+num[3]:"l", s1+num[0]:"a", s4+num[5]:"w"}

something = 1
while True:
var = str(a[t[something]])
print var,
// I want it to change "@7704@7002@7075@7704" into "lawl"

I get the error:
Traceback (most recent call last):
File "C:/Documents and Settings/Danny/My
Documents/python/changetext.py", line 9, in ?
var = str(a[t[something]])
KeyError: '7704'

I've explained what is needed to happen in the comments. Also, if any of
you can think of a better way to do this can you possibly tell me this?
Thanks.

text = '@7704@7002@7075@7704'
a={'7704':'l','7002':'a','7075':'w'}
u=[]
for c in text.split('@')[1:]:
u.append(a[c])

print ''.join(u)

Larry Bates
 
S

snoe

Ok there's a couple things going on here.
t = text.split('@') // splits the digits/blocks apart from each other
this will give you a list:
['', '7706', '7002', '7075', '7704']

You may want to change the line to skip the first empty value:
t = text.split('@')[1:]

Next your loop.
something = 1
while True:
var = str(a[t[something]])
print var,
// I want it to change "@7704@7002@7075@7704" into "lawl"

I think what you want to do here is loop through your list t, take the
values out of the encrypted text and lookup that value in a.

In python, you can loop through a list like this:

for encrypted_char in t:
var = a[encrypted_char]
print var,

now instead of printing each char as you decrypt it you should collect
them first and do the print at the end. So you get:

temp_list = []
for encrypted_char in t:
var = a[encrypted_char]
temp_list.append(var)
print ''.join(temp_list)

This still won't help with the key error you're getting though, but you
can catch that error by surrounding your offending line with a
try/except block:

temp_list = []
for encrypted_char in t:
try:
var = a[encrypted_char]
temp_list.append(var)
except KeyError:
print encrypted_char, "not in", a
temp_list.append('?')
print ''.join(temp_list)

So your final script looks like this:
text = '@7704@7002@7075@7704' # some text
num = '213654' # Number
s1 = '700'
s2 = '770'
s4 = '707' # it adds these later on.
# splits the digits/blocks apart from each other
t = text.split('@')[1:]
a = {s2+num[3]:"l", s1+num[0]:"a", s4+num[5]:"w"}

temp_list = []
for encrypted_char in t:
try:
var = a[encrypted_char]
temp_list.append(var)
except KeyError:
print encrypted_char, "not in", a
temp_list.append('?')
print ''.join(temp_list)

Fixing either your initial text or your a dict depends on your
requirements.
 
C

Colin Fox

Hello again,

I am now trying to make something to change some "encrypted" text into
some plain text, here is the code I have so far:

text = '@7704@7002@7075@7704' // some text
num = '213654' // Number
s1 = '700'
s2 = '770'
s4 = '707' // it adds these later on.
t = text.split('@') // splits the digits/blocks apart from each other
a = {s2+num[3]:"l", s1+num[0]:"a", s4+num[5]:"w"}

Well, your num[3] is going to return '6', not '4', so your key lookup is
going to fail right there. Same with num[5], which is 4, not 5.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,282
Messages
2,571,404
Members
48,096
Latest member
Kenkian2628

Latest Threads

Top