-1

I am facing a problem with my code, and I cant think of a way to fix it.

Note: I am not allowed to use import in my code

the code goes as follows:

def decode(in_file, out_file):
    try:
        s = ""
        chr_nums = []
        splitlst = []
        lines = []
        lst = []
        f = open('q4.txt','r')
        for line in f:
            lines = line.split()
            for i in range(len(lines)):
                b = str(lines[i])
                for l in b:
                    chr_nums = chr(ord(l) - 1)
                    for a in chr_nums:
                        c =' '.join(a)
                        print c 

                    
    except IOError:
        print "Cant decipher' {"+in_file+"} 'due to an IO Error."
        f.close()

The goal with this code is to replace each letter in every word to the letter before it. i.e: a is changed to z and so on.

The code has to change a file that contains this kind of text:

Uif Xiffmt po uif cvt hp spvoe boe spvoe
Uif Xiffmt po uif cvt hp spvoe boe spvoe

to this result:

The Wheels on the bus go round and round
The Wheels on the bus go round and round

this is what my for loop prints in the out_file:

T h e W h e e l s o n t h e b u s g o r o u n d a n d r o u n dT h e W h e e l s o n t h e b u s g o r o u n d a n d r o u n d

How can I get the result that I showed in the example? How can I re-join the characters to form the original order?

note2: I did try to use join, but no luck using that as well

note3: the file the code gets doesn't necessarily contain the same sentence twice.

Heba Masarwa
  • 57
  • 1
  • 7

2 Answers2

0

use if/else in Python's list comprehension as mentioned here.

example code is

f = open('q4.txt','r')
for line in f:
    lst = list(line)
    lst2 = [chr(ord(x) - 1)  if x != ' ' else ' ' for x in lst]
    print("".join(lst2))

I got the following output

The Wheels on the bus go round and round    
The Wheels on the bus go round and round
[Finished in 0.1s]
Prince Francis
  • 2,995
  • 1
  • 14
  • 22
  • thank you so much for the help! um but the thing is when i try to write the result onto a file it only prints one line when it is supposed to print as many as the original file,I am wondering if you can help me with that as well? – Heba Masarwa Dec 06 '18 at 14:11
  • 1
    open the file in `a` (append) mode as described [here](https://www.w3schools.com/python/python_file_write.asp) and instead of printing into console, use `out.write("".join(lst2))`. – Prince Francis Dec 06 '18 at 14:19
0

In addition to Prince Francis code, here is a solution that adopts your style of code:

def decode(foo):
    final_string = ""
    f = foo
    for line in f:
        lines = line.split()
        for i in range(len(lines)):
            b = str(lines[i])
            for l in b:
                chr_nums = chr(ord(l) - 1)
                final_string += chr_nums
            final_string += " "
        final_string += "\n" # new line
    return(final_string)

secret_text = ["Uif Xiffmt po uif cvt hp spvoe boe spvoe", 
               "Uif Xiffmt po uif cvt hp spvoe boe spvoe"]

print(decode(foo=secret_text))

Note how the first five lines after "try" are not needed at all. They have no impact. The rest is just setting spaces and the new-line-character when appropriate.

offeltoffel
  • 2,691
  • 2
  • 21
  • 35