2

So lets say I have two functions called func1 and func2

def func1():
a=1
return a
def func2():
    b=2
    return b
def func3():
    x=a+b
    return x
func3()

Now calling func3 returns error. Where is the problem? I have just started learning python and can't seem to solve the problem.

Farhan Bin Amin
  • 56
  • 3
  • 11
  • You can't do what you want this way. I suggest you learn about what we call "scope" of a variable and about passing parameters to a function. You will have to find another solution to your problem that works within the rules of Python. – Code-Apprentice Apr 14 '20 at 15:10
  • 1
    Sorry for replying late. After a month now that I am seeing my question, I feel really silly. I just needed parameters to pass the information. Nevertheless thanks. – Farhan Bin Amin May 26 '20 at 10:19

2 Answers2

2

The variables a and b do not exist inside func3. I suggest you pass them as parameters to the function

1

Its difficult to see what you are after, but for the code you have posted this is a solution:

def func1():
    a = 1
    return a
def func2():
    b = 2
    return b
def func3():
    x = func1() + func2()
    return x
func3()
quamrana
  • 33,740
  • 12
  • 54
  • 68