views:

426

answers:

5
exepath = os.path.join(file location here)
exepath = '"' + os.path.normpath(exepath) + '"'

results = []

for e in ('90','52.62263','26.5651','10.8123'):
if e == '90':
    z = ('0',)
elif e == '52.62263':
    z = ('0', '72', '144', '216', '288')
elif e == '26.5651':
    z = (' 324', ' 36', ' 108', ' 180', ' 252')
else:
    z = (' 288', ' 0', ' 72', ' 144', ' 216')

for a in z:

    cmd = exepath + ' -e ' + str(e) + ' -a ' + str(a) + ' ' + sys.argv[1] + ' ' + sys.argv[2]
    print cmd
    process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)

    outputstring = process.communicate()[0]
    outputlist = outputstring.splitlines()

    for i in outputlist:
        if i.find('The student says the area is:') != -1:
            print i
            results.append(i)
        print i

for s in results:
    print s

This is part of my code right now. It works perfectly fine. It scans the output of the exe file when fed through the numbers i give it in the for loop and spits out the line's that say The student says the area is 16 times for the group (90,0),(52.62263,0),...... etc. The only thing I want to fix is when it prints out the lines "The student says the area is", I want it just to print out for example (90,0) or whatever for whichever line it pertains to so it can be easily read. Any code would be helpful. I'm thinking about copying and pasting the for loop with the numbers and somehow calling it but not sure how. Thanks ahead of time. And of course i've imported everything correctly.

Right now it prints:

The Student says the area is: (" The Calculated number" ) ...x16 times

I just want the first line for example to print out:

The Student says the area is: (" The Calculated number" ) 90,0

doesn't have to be neat just the number printed out the side

the next one should be

The Student says the area is: (" The Calculated number") 52.62263,0

...x16

+4  A: 

Since it's homework, I won't provide you with a straight-out answer, but I recommend you to have a look at the Python documentation for strings. Note the syntax for getting a slice of a string (aka substring), which is probably what you should be attempting.

Blixt
Someone must have tagged it as homework. It's not homework.
Tyler
@Tyler: is that someone you? It would have shown an edit if it was retagged by someone else.
Tom
Ah, well in any case you can do with the exercise ;) Just look at the slicing syntax, it's used to get part of a string, and you want whatever comes after "The student says..."
Blixt
Thanks for not being rude Blixt. Some people shouldn't post replies if they are not willing to help. Thank you
Tyler
@Tyler: I don't think anyone is trying to be rude... but your acting like it's not hw, and you tagged it as such yourself! Not to mention you have asked previous questions with similar code snippets and admitted that it was part of a hw assignment. Maybe you're working with this code on your own and it's not hw anymore... but don't pretend you didn't tag it as such. And if it is hw, people are still happy to help you out with it as long as you're open about it.
Tom
well i tagged it but it's not homework. I tagged it only because I've asked the queestion before and it was tagged as homework so I thought some people unlike "triptych" would be helpful
Tyler
@Tyler: Stop being agressive and defensive when people try to help you.
Lennart Regebro
@Lennart help me then.
Tyler
@Tyler: We did already. If you have more questions, post them, and we'll help you there too.
Lennart Regebro
+1  A: 

I'm not completely sure I understand what question you're asking, but if you just want a line number from outputlist, you can keep your own counter:

idx = 0
for i in outputlist:
  if i.find('The student says the area is:') != -1:
    print idx, ": ", i
    results.append(i)
  idx += 1

And that should print something like:

15: The student says the area is 16 times for the group (90,0),(52.62263,0)

If that isn't what you want, perhaps you can clarify your question.

Nick Bastin
You can achieve this better with the built-in enumerate() function. "for idx, item in enumerate(outputlist):"... then you don't have to have the idx variable from before, and you can still say "print idx, ": ", item"
Tom
Ah, fair point. I don't end up doing it a lot (or possibly ever...) so I tend to miss those little idioms.
Nick Bastin
+9  A: 
for e in ('90','52.62263','26.5651','10.8123'):
if e == '90':
    z = ('0',)
elif e == '52.62263':
    z = ('0', '72', '144', '216', '288')
elif e == '26.5651':
    z = (' 324', ' 36', ' 108', ' 180', ' 252')
else:
    z = (' 288', ' 0', ' 72', ' 144', ' 216')

Hmm. I think you want to look at the section in the Python tutorials about dictionaries.

Lennart Regebro
My code works fine. I just want to print out numbers beside my output like I mentioned above
Tyler
"my code works fine" is not a good defense to bad code. I guess they teach that second year.
Triptych
it prints out what I want it to..... Is that better?Maybe you should quit wasting mine and your time and do something better than posting non helpful material ??That's all you have done on this question is ridicule my question. If you want to post please post something helpful. My code print what I want out to the screen. I just want something additional printed beside it... Someone will eventually understand hopefully
Tyler
@Tyler: Chill out, don't you think you are being overly agressive there? This site is not for homework.
voyager
This question isn't for homework. see below
Tyler
A dictionary would definitely make the code easier on the eye (and less subject to errors as each 'e' would have to be typed only once).
RaphaelSP
Oh well, I guess that stylistic issues do not matter for single-use code, but still...
RaphaelSP
@Tyler: That it works is no excuse for you not to learn about dictionaries. Learn about dictionaries. You will be happy you did.
Lennart Regebro
Respectfully I say, dictionaries have nothing to do with my program right now.
Tyler
@Tyler: Except for the fact that they would make your program better. You asked a question, you got an answer. Why are you complaining?
musicfreak
@Tyler: yes it does. See http://stackoverflow.com/questions/1121059/python-for-loop-question/1121778#1121778 And please, tone down a bit, you are coming a bit aggressive, even if it's not your intention.
voyager
And respectfully I say: That should be solved with a dictionary. Learn about them.
Lennart Regebro
+1  A: 

If all you need is the number printed out:

print '%s %s, %s' % (i, e, a)

This assumes that e and a are the numbers you want (or that's what I can tell based on a quick read).

ars
when adding print s + ' %s %s, %s"it only prints out The student says the area is: "The calculated number" 10.8123, 216 16 times. The number calculated will be different each time and needs the pertaining numbers associated with it besides it. (just for clarification to the reader of the output)
Tyler
+11  A: 

Note: A big part of writing python is to make your code readable, so in the future you might try using more descriptive variable names. PEP8 is the go-to solution for formatting python.

Another note: Questions that are asked clearly and concisely tend to get a better response from the Stack Overflow community. The less we have to work to understand you, the faster and more accurate we are in our response.

Resources:

  1. Dictionaries
  2. String formatting operations
  3. "in" keyword

Since you are adamant about the question not being homework, here is a cleaner version of your code with what I think you were asking added in.

Code:

import os
from os import popen

exepath = os.path.join(file location here)
exepath = '"' + os.path.normpath(exepath) + '"'

# This format is a little cleaner than the if-else statement.
# A dictionary (key=e, value=z) that holds your data.
data = {
        '90': ('0',),
        '52.62263': ('0', '72', '144', '216', '288'),
        '26.5651': (' 324', ' 36', ' 108', ' 180', ' 252'),
        '10.8123': (' 288', ' 0', ' 72', ' 144', ' 216'),
       }

results = []

# This is how you loop over (key, value) pairs in a dictionary
for e, z in data.iteritems():

    for a in z:
        cmd = ' '.join([exepath, '-e', str(e), '-a', str(a), sys.argv[1], sys.argv[2]])
        #or you could use the following (string substitution):
        #cmd = '%s -e %s -a %s %s %s' % (exepath, e, a, sys.argv[1], sys.argv[2])
        print cmd
        process = popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)

        output_list = process.communicate()[0].splitlines()

        for output in output_list:
            # the "in" keyword is more readable
            if 'The student says the area is:' in output:

                # this is, I believe, the line you were asking about...
                # should look like:
                # "The Student says the area is: ("The Calculated number") 90,0"
                formatted_output = '%s %s,%s' % (output, e, a)

                print formatted_output
                results.append(formatted_output)
            print output

for student_answer in results:
    print student_answer
tgray