2

In the example below i would like to use the output from fun1 in fun2. I get an error stating x is not defined. This is something extremely simple but i just don't get it.

def fun1():
    x = input('put here the value')
    return x

fun1()

def fun2(x):
  y = x + 2
  print(y)

2 Answers 2

4

You can assign it into a variable

x = fun1()
fun2(x)

Or just directly pass the result into the next function

fun2(fun1())
Sign up to request clarification or add additional context in comments.

3 Comments

It'd be nice if you also added an explanation regarding why the OP's attempt is failing (scope and not storing variables). I'd add an answer myself explaining that but seeing as this answer is already so popular it's unlikely I'd get any upvotes or be seen.
Thanks for the reply. This was quite helpful. What if the first function return 3 strings ( 'a', 'b', 'c'). I am trying to use those variables as an input for the second function.
first you would capture them, like x, y ,z = fun1() then pass them as a parms of fun2.
1

From your own code:

def fun1():
    x = input('put here the value')
    return x

x = fun1()

def fun2(x):
  y = x + 2
  print(y)

fun2(x)

Or you could take another approach, by calling the fun1() from fun2 and removing the param x.

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.