tags:

views:

133

answers:

4
take = raw_input('Please enter the string of numbers that compose code\n\n\t')
y = str(take)
l = []
for i in xrange(0, len(y), 3):
l.append(str(y[i:i+3]))
b = len(l)
a = 0

while(a!=b):
    c = l[a].replace('444', ' ')
    c = l[a].replace('111', 'a')
    c = l[a].replace('112', 'b')
    c = l[a].replace('113', 'c')
    c = l[a].replace('114', 'd')
    c = l[a].replace('115', 'e')
    etc...
    a = a + 1

filename = 'decmes.txt'
file = open(filename, 'w')
file.write(c)
file.close()

I can enter anything, just 111 for example and it gives me back the same thing I put in. Maybe it's something dumb, but I can't figure it out. Suppose i wanted it to say bad, I would type 112111114. It should give me bad, but it doesn't.

A: 

It looks to me like you're repeatedly overwriting c.

So your final result would be this (for the highest value of a):

c = l[a].replace('115', 'e')

(assuming that's the last replacement in the sequence).

All of the other lines of code in that sequence would have no effect.

Robert Harvey
if i type 111 it will only replace c with 111.
Kyle W
+2  A: 

It's not immediately obvious to me what you are trying to do, but maybe you mean this?

c = ''
while(a!=b):
        c += l[a].replace('444', ' ') \
                 .replace('111', 'a') \
                 .replace('112', 'b') \
                 .replace('113', 'c') \
                 .replace('114', 'd') \
                 .replace('115', 'e')
        a = a + 1

There are a number of style issues here though. Here are some examples:

  • Your line y = str(take) is unnecessary. take is already a string.
  • You should use something like a dictionary define the replacements instead of a long list of replace statements.
  • You could consider using the grouper recipe from itertools instead of writing it yourself.
  • Your while loop is very C like. Consider using for x in l: instead.
Mark Byers
I added to the bottom. Maybe it helps
Kyle W
A: 

This code makes my mind explode in agony. However, I think this will be your solution:

Before the while loop, do a c="" then change the c = to c +=

Legatou
A: 

You absolutely need to get familiar with dictionaries!

Try sth like

code_dict = {'444': ' ',
             '111': 'a',
             '112': 'b',
             ...}

and then make your replacements via

c = "".join([code_dict[part] for part in l])

instead of the whole while a!=b loop.

jellybean