0

How can i get the output from another script?

My first script to run:

from test2 import *
class Test():
    def todo (self):
        mult()
        addx()

if __name__ == '__main__':
    Test().todo()

My second script named (test2.py):

def mult():
    x= 2 * 4
    print(x)
    return x

def addx():
    sum = x + 2
    print("sum",sum)

Error:

NameError: name 'x' is not defined

3 Answers 3

1

In the function addx() you haven't declared x. I believe you want x from mult. So you can do something like this

def addx():
    x = mult()
    sum = x + 2
    print("sum",sum)
Sign up to request clarification or add additional context in comments.

2 Comments

can i get the value directly that the mult function returns without adding the line x = mult() ?
not without calling mult(). You could however do sum = mult() + 2, that way not having to use the variable x
0

You should use the return value of mult, to pass it to your second function addx as a parameter.

def todo (self):
    x = mult()
    addx(x)

I advise you to read the Python doc section about function : https://docs.python.org/fr/3/tutorial/controlflow.html#defining-functions

Comments

0

In test2.py, you have not defined x

def addx():
sum = x + 2
print("sum",sum)

The problem above is that the computer doesn't know what x is. You could pass it as a parameter:

def addx(x):
sum = x + 2
print("sum", sum)

and change your code to:

from test2 import *

class Test():
    def todo(self):
        addx(x=mult()) # whatever number you want

if __name__ == '__main__':
    Test().todo()

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.