0

So I have this program. Basically I get 7 coordinates and I sent back 1 coordinate. (Cant really explain)

from sys import stdin ,stdout

test=int(stdin.readline().strip("\n")) #It got stripped the new line

for z in range(test):
    c=stdin.readline().strip("\n").split(" ")# Here also!
    x=[0,0,0,0,0,0,0,0]
    y=[0,0,0,0,0,0,0,0]
    letter="abcdefgh"
    for k in range(7):
        x[letter.index(c[k][0])]=1
        y[int(c[k][1])-1]=1
    stdout.write(letter[x.index(0)]+str(y.index(0)+1)+"\n") #This is the main bits

Here's the thing.

This program respond is caught by another program and written in a file. When I use stdout.write(+"\n") or print, my program outputs something like

h4

g2

h4
...

But when I just use plain stdout.write()

h4g2h4...

How do I make my program outputs something like this:

h4
g2
h5
...

The program who records my program uses this:

result=open("txt.txt","w")
process=subprocess.Popen([sys.executable,"work.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
res=process.communicate(a)
result.write(res[0])

As far as I concern, print adds 1 line, not 2 lines. That's where I got lost.

My input does have new line, but it got stripped, as you can see in line 2.

Realdeo
  • 449
  • 6
  • 19
  • Ok, where I do wrong so I get the downvote? – Realdeo Aug 29 '14 at 17:35
  • I didn't down vote but I'm guessing it was because parts of your question are not easy to understand and the answer could easily be figured out with by looking at your input and examining it as you process it. – cmd Aug 29 '14 at 17:39

1 Answers1

1

So since you are writing to a file I can sort of answer the question based upon my knowledge of python. Correct me if I am wrong, but here is my answer:

stdout.writelines([letters[x.index(0)]+str(y.index(0)+1)])

This could not work very well as your x values change and so forth.

You could use .format like so :

stdout.write('{} \n'.format(letters[x.index(0)]+str(y.index(0)+1)))

However if you can't make that work, then it may be how you are formatting stdout.write()...

Colin

Colin
  • 329
  • 2
  • 14