1

The first python program called first.py includes the following code:

print ('my name is viena')

How can I input the result from that first program, i.e., 'my name is viena' as input into my second program called second.py which includes the following code:

question = 'what\'s your name?'
answer = ?????HERE HOW TO INPUT THE RESULT FROM FIRST PROGRAM

print (answer)
my name is viena

Note: There should be two different python script files as I mentioned in the question.

2
  • 1
    Create a function which ll return the value in file1. In file2, import file1 and call the function? Commented Jan 16, 2014 at 6:51
  • Don't put a space between print and (.... Commented Jan 16, 2014 at 6:56

2 Answers 2

1

If you can wrap the print statement in your first program inside a function (say print_name) , then you can so something like

import first
first.print_name()

to get the name.

For a completely general purpose solution that involves shelling out, take a look at the subprocess module.

Sign up to request clarification or add additional context in comments.

Comments

1

How about change the first script as importable module:

# Changed the print function call with function.
def my_name():
    return 'my name is viena'

if __name__ == '__main__':
    print(my_name())

Another script can now import the first script using import statement, and use its functions.

import name_of_the_first_script

question = "what's your name?"
answer = name_of_the_first_script.my_name()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.