1
def func1():
    x = 100
    john = 'hello'
    return x, john

def func2():
    func1()
    y = x
    return y

print(func2())

So this returns an error:

NameError: name 'x' is not defined

Can someone explain how to use variables of func1 in func2, and explain how calling func1 in func2 work.

1
  • You cannot use x in func2 because it is only available in func1(). Whatever variable you define locally in a function will never be accessible to another. X would have to be a static variable to be used in other functions. Define x outside of any function. Commented Oct 6, 2017 at 20:09

2 Answers 2

1
def func1():
    x = 100
    john = 'hello'
    return x, john

def func2():
    x, john = func1()
    y = x
    return y

print(func2())

x is local to func1 (so as to john). But it is one of the returned values of the function; so use it!

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

Comments

1
def func1():
    x = 100
    john = 'hello'
    return x, john

def func2():
    x,john=func1()
    y = x
    return y

print(func2())

If you are returning two variables from func1 then the result of func1 should be passed to some other variables too. So you should add:

x,john=func1()

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.